Search code examples
c#winformsbuttongroupbox

Loading multiple Groupboxes in the form using the button_click event


I wish to load multiple groupboxes in the windows form application using a button_click event.

A groupbox should appear in the form each time the button is clicked.

Screenshot of my expected output Expected output.


I am having trouble making the location of the groupbox dynamic, as the second groupbox should be some distance away from the first groupbox. I thought of manually calculating the coordinates and using an array of points for the location, but I feel that there should be a better a way to go about it.

I have defined 'int count=0' variable to count the number of times the button is clicked. Based on that I am naming the new groupbox. But I think there is some problem in the logic used in the count++ line. It is not going after 1. Therefore I am only getting one groupbox "groupBox1". Nothing happens when I click the button again.

I appreciate your help.

Thank you

int count=0;
private GroupBox GetGroupBox(int a)
{
     GroupBox groupBox = new GroupBox();
     groupBox.Text = "groupBox"+(a.ToString());
     groupBox.Width= 200;
     groupBox.Height= 200;
     groupBox.Location = new Point(50,400);
     return groupBox;            
 }
 private void button1_Click(object sender, EventArgs e)
 {              
     count++;                       
     this.Controls.Add(GetGroupBox(count));           
 }

Solution

  • Since you want to create boxes from left to right you should adjust Left: say, 1st box should have Left = 50, 2nd Left = 270, 3d Left = 490 etc.

    Code:

    const int deltaX = 20;
    ...
    //TODO: check do you really want Top = 400, not, say, 20?
    groupBox.Location = new Point(50 + (a - 1) * (groupBox.Width + deltaX), 400);
    ...
    

    Simplified implementation can be

    int count = 0;
    
    // Let's rename the method: we actually create GroupBox, not get existing
    private GroupBox CreateGroupBox(int index) => new GroupBox() {
      Text     = $"groupBox{index}",
      Size     = new Size(200, 200),
      Location = new Point(50 + (index - 1) * (20 + 200), 400),
      Parent   = this, // Instead of Controls.Add()
    };
    
    private void button1_Click(object sender, EventArgs e) {
      CreateGroupBox(++count);  
    }