Windows form Find control
I want to Find All Textboxes in one Windows Form and i need to make all textboxes Readonly.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nilanka DharmadasaPosted Nov 20, 2009, 8:26 AM
The code given above will not work for the textboxes, you placed inside container controls like Panel, Group Box etc..
So you shud call a recursive function as I have mentioned below.
Try my code. It will resolve your problem.
According to my example, when you click on the button, all the text boxes will be readonly.
private void button1_Click(object sender, EventArgs e)
{
MakeReadOnly(this);
}
private void MakeReadOnly(Control control)
{
if (control is TextBox)
{
((TextBox)control).ReadOnly = true;
return;
}
if ((control.Controls == null) || control.Controls.Count == 0)
return;
foreach (Control c in control.Controls)
{
MakeReadOnly(c);
}
}
If you find my answer useful, please do not forget to mark my answer as Accepted.
Roei BarPosted Nov 20, 2009, 6:33 AM
Lalit MPosted Nov 20, 2009, 5:32 AM
Control myControl = new Control ();
foreach (Control c in this.Controls)
if (c.Name == "textBox1")
{
myControl = c;
MessageBox.Show(myControl .Name );
}