简体   繁体   中英

Inserting a Combobox Inside a groupbox that's inside a container

I created a flowlayout panel in design view and I want to place in it a dynamic number of groupboxes, each in turn having a ComboBox inside it. The last line (one with the slashes above and below it) is the one which is troubling me; I don't really know why it does not work.

PS: When I draw the groupboxes in design view, I am able to place the Comboboxes inside!

            List<GroupBox> GroupBoxes = new List<GroupBox>();
            List <ComboBox> Caja =new List<ComboBox>();
            for (int i = 0; i < Campos.Count; i++)
            {
                GroupBoxes.Add(new GroupBox());
                Caja.Add(new ComboBox());
            }
            for (int i = 0; i< Campos.Count; i++)
            {
                //Agregamos la caja...

                GroupBoxes[i].Location = new System.Drawing.Point(51, 21);
                GroupBoxes[i].Size = new System.Drawing.Size(203, 56);
                GroupBoxes[i].Text = "haha";
                GroupBoxes[i].Name ="GroupBox"+i.ToString();
                this.flowLayoutPanel1.Controls.Add(GroupBoxes[i]);


                Caja[i].Location = new System.Drawing.Point(51, 21);
                Caja[i].Name = "comboBox"+i.ToString();
                Caja[i].Size = new System.Drawing.Size(121, 21);
                Caja[i].DropDownStyle = ComboBoxStyle.DropDownList;

                /////////////////
                this.GroupBoxes[i].Controls.Add(Caja[i]);
                /////////////////
            }
        }

Your code should be something like this:

        List<GroupBox> GroupBoxes = new List<GroupBox>();
        List <ComboBox> Caja =new List<ComboBox>();
        for (int i = 0; i < Campos.Count; i++)
        {
            ComboBox cb = new ComboBox();
            cb.Location = new System.Drawing.Point(51, 21);
            cb.Name = "comboBox"+i.ToString();
            cb.Size = new System.Drawing.Size(121, 21);
            cb.DropDownStyle = ComboBoxStyle.DropDownList;
            Caja.Add(cb);
            GroupBox gb = new GroupBox();
            gb.Location = new System.Drawing.Point(51, 21);
            gb.Size = new System.Drawing.Size(203, 56);
            gb.Text = "haha";
            gb.Name ="GroupBox"+i.ToString();
            gb.Controls.Add(cb);
            GroupBoxes.Add(gb);
            this.flowLayoutPanel1.Controls.Add(gb);
        }
    }

or just place the following line

this.flowLayoutPanel1.Controls.Add(GroupBoxes[i]);

at the end after

/////////////////
this.GroupBoxes[i].Controls.Add(Caja[i]);
/////////////////

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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