Search code examples
c#windowscontrolswindows-forms-designerpanels

C# Windows Form: Looping through Dynamically created TextBoxes and checking to see if Text has changed


I am trying to create a graphical SQL editor of sorts - but I don't like the the visuals of a table and am trying to add more interactivity(drag/drop, etc).

I've gone through and created Panels based off each record and added textboxes to each panel based on each record from my table. What I am stuck on now is the concept of looping through dynamically created Controls and checking their state or interacting with them.

Please let me know if you see issues with how I am structuring this.

My Code is as follows:

Code that Generates the Panels:

  private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        groupBox1.Controls.Clear();
        string pDBString = null;
        SqlConnection cnn;
        pDBString = "Data Source=localhost\\" + Form1.host + ";Initial Catalog=" + Form1.db + ";Integrated Security=SSPI;";
        cnn = new SqlConnection(pDBString);
        string sqlForProps = "select * from PROPS where user_id_txt ='" + comboBox1.SelectedItem.ToString() + "'";
        try
        {
            using (cnn)
            {
                cnn.Open();
                SqlCommand cmd = new SqlCommand(sqlForProps, cnn);
                SqlDataReader sqlReader = cmd.ExecuteReader();

                int x = 0;
                int count = 0;
                while (sqlReader.Read())
                {
                    Panel panel = new System.Windows.Forms.Panel();
                    panel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                    x += 30;
                    panel.Location = new System.Drawing.Point(3, x);
                    panel.Name = "panel" + count;
                    panel.Size = new System.Drawing.Size(519, 30);
                    panel.TabIndex = 3;
                    PropsPanels.Add(panel);
                    groupBox1.Controls.Add(panel);

                    TextBox textbox = new System.Windows.Forms.TextBox();
                    panel.Controls.Add(textbox);
                    textbox.Location = new System.Drawing.Point(1, 3);
                    textbox.Name = "textBox" + count;
                    textbox.Size = new System.Drawing.Size(100, 20);
                    textbox.TabIndex = 4;
                    textbox.Text = sqlReader["USER_ID_TXT"].ToString();

                    TextBox textboxAM = new System.Windows.Forms.TextBox();
                        panel.Controls.Add(textboxAM);
                        textboxAM.Location = new System.Drawing.Point(126, 3);
                        textboxAM.Name = "textBoxAM" + count;
                        textboxAM.Size = new System.Drawing.Size(100, 20);
                        textboxAM.TabIndex = 4;
                        textboxAM.Text = sqlReader["PROP_TXT"].ToString();

                    TextBox textboxAMSet = new System.Windows.Forms.TextBox();
                        panel.Controls.Add(textboxAMSet);
                        textboxAMSet.Location = new System.Drawing.Point(232, 3);
                        textboxAMSet.Name = "textBoxAM" + count;
                        textboxAMSet.Size = new System.Drawing.Size(100, 20);
                        textboxAMSet.TabIndex = 4;
                        textboxAMSet.Text = sqlReader["VAL_TXT"].ToString();
                    count++;
                }
                sqlReader.Close();
                cnn.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection !");
        }

    }

The code that is suppose to be checking the panels:

        public AMMain()
    {

        InitializeComponent();
        string pDBString = null;
        SqlConnection cnn;
        pDBString = "Data Source=US7-AHACKETT\\SQLEXPRESS;Initial Catalog=OrchestroConfigurationDB;Integrated Security=SSPI;";
        MessageBox.Show(pDBString);
        cnn = new SqlConnection(pDBString);
        try
        {
            using (cnn)
            {
                SqlCommand sqlForUserList = new SqlCommand("select UserName from users a join Company b on a.CompanyID = b.CompanyID where CompanyCode='" + Form1.company + "'", cnn);
                cnn.Open();
                MessageBox.Show("Connection Open !");
                SqlDataReader sqlReader = sqlForUserList.ExecuteReader();
                while (sqlReader.Read())
                {
                    comboBox1.Items.Add(sqlReader["UserName"].ToString());
                }
                sqlReader.Close();
                cnn.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection !");
        }

        foreach (Panel p in PropsPanels)
        {
            foreach (Control c in p.Controls)
            {
                if(c is TextBox)
                {
                    object sender = new object();
                    EventArgs e = new EventArgs();
                    if(c.TextChanged()??????)
                    {
                     //DOSOMETHING   
                    }
                }
            }
        }
    }

For example: if I wanted to check if text changed for a textbox that I put on the form I would do so:

        private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

So I guess I can't wrap my head around how I would check this at run-time since I am creating the Textboxes at runtime.

Thanks for any help!


Solution

  • For example: if I wanted to check if text changed for a textbox that I put on the form I would do so:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    
    }
    

    So I guess I can't wrap my head around how I would check this at run-time since I am creating the Textboxes at runtime.

    You do the same!

    Create a method to handle the event first:

    private void TextBoxTextChanged(object sender, EventArgs e) {
        // you can use the sender argument to check exactly which text box's text changed
    }
    

    When you initialize your form, you do this:

    textbox.TextChanged += TextBoxTextChanged;
    textboxAM.TextChanged += TextBoxTextChanged;
    textboxAMSet.TextChanged += TextBoxTextChanged;