简体   繁体   English

将列表框添加到动态创建的标签页

[英]add listboxes to dynamically created tabpages

I am trying to programmatically add listboxes to dynamically created tabpages within tabcontrols. 我试图以编程方式将列表框添加到tabcontrols中动态创建的选项卡中。 I don't understand where is the problem as I it doesn't give me any error when compiling. 我不明白问题出在哪里,因为它在编译时不会给我任何错误。 I used breakpoints on the if statement if (c is TabPage) and it shows correctly the tab pages which are created. 我在if语句if (c is TabPage)上使用了断点,它正确显示了创建的选项卡页。

    public void createControls()
    {
        TabControl tabcontrol = new TabControl();
        panel1.Controls.Add(tabcontrol);
        tabcontrol.Dock = DockStyle.Fill;
        int n = 1;
        do
        {
            tabcontrol.Controls.Add(new TabPage() { Name = "Property #" + n + "", Text = "Property #" + n + "" });
            n++;
        } while (n == pnum);

        foreach (Control c in panel1.Controls)
        {
            if (c is TabPage)
            {
                ListBox list = new ListBox();
                list.Items.AddRange(new object[] {
                "Id",
                "Name",
                "Entity"});
                list.Location = new System.Drawing.Point(20, 38);
                list.Name = "listBox1";
                list.ScrollAlwaysVisible = true;
                list.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
                list.Size = new System.Drawing.Size(134, 147);
                c.Controls.Add(list);
            }
        }

    }

You need to make sure that you access the controls of the immediate container. 您需要确保您访问即时容器的控件。

foreach (Control c in panel1.Controls)

Should be 应该

foreach (Control c in tabcontrol.Controls)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM