I have a Form on which there are two controls, a Button and a TextBox.
These controls are created at runtime.
When I click the Button, I want to do some operations with the TextBox.Text property.
But, with this code I can’t:
private void Form1_Load(object sender, EventArgs e)
{
TextBox txb = new TextBox();
this.Controls.Add(txb);
Button btn = new Button();
this.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
}
Here I’m trying to find it:
public void btn_Click(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item is TextBox)
{
if (((TextBox)item).Name=="txb")
{
MessageBox.Show("xxx");
}
}
}
}
You don’t have a Textbox with Name “txb”. so, this expression will always be false:
if(((TextBox)item).Name=="txb")try this codes: