You must Sign In to post a response.
  • Category: Windows

    Get all controls using form

    How to get all controls in functions by passing input as Form alone( form name or class namel


    Function void (Form f)
    {
    F.controls // textbox or label
    }
  • #770095
    Hi Lily,

    You can get all controls using for each loop like below.

    public function void (Form f)
    {
    for each(Control c in f.Controls)
    {
    MessageBox.Show(c.Name);
    }
    }

    Hope it will help you.

    Thanks.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #770406
    Here is a piece of code that will enable you to list every control on a form. To identify the child controls for any control, use this snippet.
    public static class Extensions
    {
    public static void EnumerateChildren(this Control root)
    {
    foreach (Control control in root.Controls)
    {
    Console.WriteLine("Control [{0}] - Parent [{1}]",
    control.Name, root.Name);
    if (control.Controls != null)
    {
    EnumerateChildren(control);
    }
    }
    }
    }
    And you can invoke this function like this, to enumerate all the controls of a Form.
    this.EnumerateChildren();


  • Sign In to post your comments