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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Lalit MRank #1,186 · 1.5K reputationPosted Nov 20, 2009, 12:32 AM EST
Control myControl = new Control ();
foreach (Control c in this.Controls)
if (c.Name == "textBox1")
{
myControl = c;
MessageBox.Show(myControl .Name );
}
Roei BarRank #1,271 · 1.4K reputationPosted Nov 20, 2009, 1:33 AM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 20, 2009, 3:26 AM EST
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.