我正在处理Windows窗体,我正在“添加更多”按钮创建文本框,所以我想获取每个文本框的文本更改事件的值。
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
// I created text box wc
TextBox wc = new TextBox();
// now bind it with textchanged event
wc.TextChanged += wc_TextChanged;
}
void wc_TextChanged(object sender, EventArgs e)
{
var box = (TextBox)sender;
label1.Text = box.Text;
}
我试过这个,但是在每个盒子的文本更改中都没有被解雇,
由于
答案 0 :(得分:1)
您还需要将其添加到form
:
// I created text box wc
TextBox wc = new TextBox();
// now bind it with textchanged event
wc.TextChanged += wc_TextChanged;
this.Controls.Add(wc);
答案 1 :(得分:0)
您应该添加事件和文本框
private TextBox wc;
Form()
{
InitializeComponent();
wc = new TextBox();
wc.TextChanged += wc_TextChanged;
wc.Visible = false;
Controls.Add(wc);
}
private void button1_Click(object sender, EventArgs e)
{
wc.Visible = true;
}
应始终添加控件,您应该只控制控件的可见性!
答案 2 :(得分:0)
它的工作正常我
private void button1_Click(object sender, EventArgs e)
{
TextBox wc = new TextBox();
this.Controls.Add(wc);
wc.TextChanged+=wc_TextChanged;
}
void wc_TextChanged(object sender, EventArgs e)
{
//var box = (TextBox)sender;
MessageBox.Show("TEXT CHANGED");
}