Q:

How to get value from textBox by its name?

I am trying to get value from textBox by its name using Control class?
There is my code:
Control ctl = FindControl(this, "B1");
      if (ctl is TextBox)
        listBox1.Items.Add(((TextBox)ctl).Text);
//"B1" - it's textBox name
public static Control FindControl(Control parent, string ctlName)
    {
        foreach (Control ctl in parent.Controls)
        {
            if (ctl.Name.Equals(ctlName))
            {
                return ctl;
            }

            FindControl(ctl, ctlName);
        }
        return null;
    }

The problem is that the compiler does not go into the function.
What could be the problem?

A:

        public Form1()
        {
            InitializeComponent();
            B1.Text = "LOL";
            Control ctl = FindControl(this, "B1");
            if (ctl is TextBox)
                listBox1.Items.Add(((TextBox)ctl).Text);
        }
        public static Control FindControl(Control parent, string ctlName)
        {
            foreach (Control ctl in parent.Controls)
            {
                if (ctl.Name.Equals(ctlName))
                {
                    return ctl;
                }

                FindControl(ctl, ctlName);
            }
            return null;
        }

If you did it like in sample above then all be right.
I suppose you use Windows Froms.
P.S. I can't write a commentary because I haven't 50 reputation.
Correct Answer
If TextBoxes are on the FlowLayout then the parent is FlowLayout and you need use the FlowLayout name instead of "this" in line Control ctl = FindControl(this, "B1");. Because "this" it's the MainWindow control.

