accessing controls in container object
i want to access each and every controls in a form. for this i have written this code below.
public void clearText(Form frm){
foreach (Control cnt in frm.Controls){
if (cnt is TextBox)
cnt.Text = "";
}
}
this works if i dont have any container(groupbox,panel) control in the form.
for e.g in a form if i have 5 textbox in that 3 are placed directly on the form and 2 are placed inside container control, using the above function i can access only 3 text boxes. how can i access each and every controls in a form.
sujeethPosted Aug 30, 2006, 4:10 AM
Manoj BistPosted Aug 29, 2006, 10:59 PM
hi,
here is the complete code
I have made a recursive function...........
private void clearAllText(Control container)
{
foreach (Control cnt in container.Controls)
{
if (cnt is TextBox)
{
((TextBox)cnt).Text = "";
}
else if (cnt is GroupBox || cnt is Panel || cnt is TabControl )
{
clearAllText(cnt);
}
}
}
call this function whenever you want to access all controls of your form
clearAllText(this);