Home > OS >  Loading multiple Groupboxes in the form using the button_click event
Loading multiple Groupboxes in the form using the button_click event

Time:02-03

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));           
 }

CodePudding user response:

Your question states these objectives:

  • Dynamically add a GroupBox based on an event (like button click).
  • Assign the new GroupBox location.
  • Pad the location with "some distance away".

You say you "feel that there should be a better a way to go about it" and there is!


Try experimenting with a FlowLayoutPanel which handles all three of these by its nature.

screenshot


Here's the code I used to add and remove instances of CustomGroupBox. This is a UserControl that I added to my project, but this will work with any type of control.)

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        numericUpDownGroupboxes.ValueChanged  = onGroupBoxCountChanged;
        foreach (var radio in Controls.OfType<RadioButton>())
        {
            radio.CheckedChanged  = onFlowLayoutDirectionChanged;
        }
    }

When the numeric up-down changes, compare the expected number of groupboxes to the current count. Alternatively, you can continue to use a button click and go straight to flowLayoutPanel.Controls.Add(...).

    private void onGroupBoxCountChanged(object sender, EventArgs e)
    {
        // Need an int for comparison.
        int 
            countIs = flowLayoutPanel.Controls.OfType<CustomGroupBox>().Count(),
            countShouldBe = (int)numericUpDownGroupboxes.Value;
        switch(countIs.CompareTo(countShouldBe))
        {
            case -1:
                flowLayoutPanel.Controls.Add(
                    new CustomGroupBox
                    {
                        Name = $"groupBox{countShouldBe}",
                        Text = $"GroupBox {countShouldBe}",
                        Size = new Size(300, 150),
                        Margin = new Padding(10),
                        BackColor = Color.White,
                    });
                break;
            case 1:
                Control last = flowLayoutPanel.Controls.OfType<CustomGroupBox>().Last();
                flowLayoutPanel.Controls.Remove(last);
                break;
        }
    }

The direction of the flow can also be specified.

    private void onFlowLayoutDirectionChanged(object sender, EventArgs e)
    {
        if(radioButtonHorizontal.Checked)
        {
            flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
        }
        else
        {
            flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
        }
    }
}

CodePudding user response:

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);  
}
  • Related