C# activeform controls property

Viewed 47

I am unable to get the controls count on the newly created form on the newly created button, I have created 5 controls but only one is showing. If I cannot get the total control count then I cannot also get the control type, name etc.

private void button2_Click(object sender, EventArgs e)
{
    Form frm = new Form();
    frm.Text = "new form";
    TableLayoutPanel tlp = new TableLayoutPanel();
    tlp.AutoSize = true;
    Button btn = new Button();
    btn.Text = "ok";
    tlp.Controls.Add(btn, 0, 4);
    frm.Controls.Add(tlp);

    for (int i = 3, ii = 0; i >= 0; i--, ii++)
    {
        TextBox tbx = new TextBox();
        tlp.Controls.Add(tbx, 0, ii);
    }

    frm.Show();
    string str = frm.Controls.Count.ToString();
    btn.Click += (s, args) =>
    {
        MessageBox.Show(frm.Text);
        MessageBox.Show(ActiveForm.Text);
        MessageBox.Show(str);
    };
}
1 Answers

In your code the only Control that you've added to your form is a TableLayoutPanel that contains TextBox controls. That is why the count is 1.

Related