The code is the same, but you need to check "is this a textbox?" inside your loop:
foreach(Control c in con.Controls)
{
if (c is TextBox)
{
c.Enabled = false;
}
}
Quote:
Can it Work when my textbox is in Panel
Depends on what you pass to your method: if it's the Form, then it won't - the Form.Controls collection contains the panel, and the Panel.Controls collection contains the TextBox so it won't get found.
If it's the Panel you pass then it will.
If you want to disable a textbox "somewhere in a control on the the form", then you need to call your method recursively:
public void DisableControl(Control con)
{
foreach(Control c in con.Controls)
{
DisableControl(c);
if (c is TextBox)
{
c.Enabled = false;
}
}
}
That will work if you pass it the form, and disable all textboxes on the form.