简体   繁体   中英

Create GroupBox and labels inside it programmatically

I want to create a groupbox programmatically and inside it put labels. Now I created this but with design and I have like this:

在此处输入图片说明

I'm trying but I don't know how can I assign labels in correct position and how can I assign it to a specific group box

GroupBox groupBox1 = new GroupBox();
Panel grid1 = new Panel();
Label lbl1 = new Label { Text = "Completed" };
Label lbl2 = new Label { Text = "label" };
Label lbl3 = new Label { Text = "In progress" };
Label lbl4 = new Label { Text = "label" };
//etcetera
groupBox1.Width = 185;
groupBox1.Height = 160;
grid1.Height = 185;
grid1.Width = 160;

How can I achieve that? Regards Update

As the comments below I try

GroupBox groupBox1 = new GroupBox();
                    this.Controls.Add(groupBox1);
                    Panel grid1 = new Panel();
                    groupBox1.Controls.Add(grid1);
                    groupBox1.Location = new Point(20, 250);
                    grid1.Location = new Point(20, 250);
                    Label lbl1 = new Label { Text = "test" };
                    Label lbl2 = new Label { Text = "Test2" };
                    groupBox1.Name = "TESTTT";
                    groupBox1.Width = 222;
                    groupBox1.Height = 149;

                    grid1.Height = 218;
                    grid1.Width = 145;
                    grid1.Controls.Add(lbl1);
                    grid1.Controls.Add(lbl2);

Result: 在此处输入图片说明

But my group box it just clear without name and without labels, why it happen?

Controls in WinForms are arranged so that they reside inside one another. So your Form has a Controls collection which is essentially a Collection of type Control . So if you add a GroupBox to the form, then you must add it to the Controls collection of the form. Then if you add a control to your GroupBox then you need to add it to the GroupBox collection of controls.

With that in mind, you can do something like this:

private void AddGroupBoxAndLables()
{
    GroupBox groupBox1 = new GroupBox();
    groupBox1.SetBounds(50, 50, 300, 200);
    this.Controls.Add(groupBox1);

    Label lblCompleted = new Label { Name = "lblCompleted", Text = "Completed" };
    lblCompleted.Location = new Point(20, 20);
    groupBox1.Controls.Add(lblCompleted);
    Label valCompleted = new Label { Name = "valCompleted" };
    valCompleted.Location = new Point(80, 20);
    groupBox1.Controls.Add(valCompleted);

    Label lblInProgress = new Label { Name = "lblInProgress", Text = "In Progress" };
    lblInProgress.Location = new Point(20, 60);
    groupBox1.Controls.Add(lblInProgress);
    Label valInProgress = new Label { Name = "valInProgress" };
    valInProgress.Location = new Point(80, 60);
    groupBox1.Controls.Add(valInProgress);
}

simplest way for solution is that your code is already created when you did design. Code is created automatically in respective form's designer.cs or designer.vb file. To see this file, in solution explorer click on button 'Show all files'. Still for your understanding let me explain code

You have created groupbox using GroupBox groupBox1 = new GroupBox(); To see this group box on form you need to add this groupbox to form this.Controls.Add(groupBox1);

Similar in case of panel. If you want to add panel inside of groupbox then groupbox1.Controls.Add(grid1);

Then add all labels inside of panel.

You will find similar kind of code in form's designer.cs.

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