C#Windows窗体:通过动态创建的TextBox循环并检查Text是否已更改

时间:2017-02-01 16:36:14

标签: c# windows controls windows-forms-designer panels

我正在尝试创建各种图形化SQL编辑器 - 但我不喜欢表格的视觉效果,并且正在尝试添加更多交互性(拖放等)。

我已经完成并根据每条记录创建了面板,并根据我桌子上的每条记录为每个面板添加了文本框。我现在坚持的是循环动态创建的控件并检查其状态或与它们交互的概念。

如果您发现我如何构建此问题,请告诉我。

我的代码如下:

生成面板的代码:

  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 !");
        }

    }

假设要检查面板的代码:

        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   
                    }
                }
            }
        }
    }

例如:如果我想检查文本框中是否更改了文本,我会这样做:

        private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

所以我想我无法在运行时检查这个问题,因为我在运行时创建了Textboxes。

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

  

例如:如果我想检查文本框中的文本是否已更改   把它放在表格上我会这样做:

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
     

所以我想我无法解决这个问题   运行时,因为我在运行时创建文本框。

你做同样的事情!

首先创建一个处理事件的方法:

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

初始化表单时,请执行以下操作:

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

答案 1 :(得分:0)

您可以像手动将文本框放入原位一样执行此操作。您只需订阅每个新的TextBox的{​​{1}}活动。

TextChanged

在旧版本的.Net中,您可能需要指定委托。有关详细信息,请参阅MSDN on the subject

答案 2 :(得分:0)

在创建控件时,您需要在TextChanged委托上注册。

TextBox textbox = new System.Windows.Forms.TextBox();
panel.Controls.Add(textbox);
textbox.Location = new System.Drawing.Point(1, 3);
textbox.Name = "textBox" + count;
textbox.TextChanged += TextBox_TextChanged

然后,在事件处理程序中,使用sender参数强制转换为触发事件的控件实例。

  private void(object sender, EventArgs e)
    {
        //get name of textbox
        var tb = (TextBox) sender;
        // do whatever with text box
    }