Skip to content

Accessing controls in container object

sujith

Asked Aug 27, 2006, 11:18 PM EDT2 answers
Share
LinkedInX
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.

  • 0

    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);  


    0
  • 1 short reply
    • 0
      thanks for the reply manoj
      0