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.
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Manoj Bist15 pointsPosted Aug 29, 2006, 6:59 PM EDT
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);
1 short reply
sujeeth5 pointsPosted Aug 30, 2006, 12:10 AM EDT