4

I'm creating an application on Visual Studio 2012 (c#/Winforms). What exactly i have in mind is depicted by the image: enter image description here

When Button1 is clicked the content within the groupbox will change, same with button 2 & 3.

I'm currently using the following:

private void button2_Click(object sender, EventArgs e)
{
    this.Controls.Remove(this.groupBox1);
    this.Controls.Add(this.groupBox2);
}

My Questions are:

a) By using this, will runtime performance will be hampered as all controls will be active(though hidden) at the same time?

b) Consider I continue to using the current method, Can I create a new workspace and build each groupbox in different windows?

c) Is there are workaround to my current method?

Thanks.

nawfal
  • 70,104
  • 56
  • 326
  • 368
Snak3
  • 43
  • 1
  • 5
  • This is a good example of when to use an [MDI](http://www.codeproject.com/Articles/7571/Creating-MDI-application-using-C-Walkthrough) – Leon Newswanger Feb 02 '13 at 07:06

1 Answers1

4

Better would be just:

private void button2_Click(object sender, EventArgs e)
{
    groupbox2.Visible = true;
    groupbox1.Visible = !groupbox2.Visible;
}

and similarly

private void button1_Click(object sender, EventArgs e)
{
    groupbox1.Visible = true;
    groupbox2.Visible = !groupbox1.Visible;
    groupbox3..... = !groupbox1.Visible; //etc
}

This will be more performant, and also adding and removing controls can have side effects with complex guis where controls wont get positioned properly.

To answer you:

a) Not much, but as I said its better to toggle the visibility.

b) What is a workspace? Of course you can ahead with your approach from any window or form. But if you are adding groupboxes of one form to another, then groupboxes from the first form will be removed.

c) My answer..

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • Thanks. I'd like to toggle the visibility but building the gui on the designer will be impossible with multiple groupboxes one over the other. By workspace i meant a new window/tab, so I could build each groupbox separately. Or can i hide the other groupboxes in the designer? – Snak3 Feb 02 '13 at 06:19
  • Do you want to add groupboxes dynamically? – nawfal Feb 02 '13 at 06:20
  • Yes, that too would be preferable. – Snak3 Feb 02 '13 at 06:22
  • In that case, yes add them to appropriate controls, but still to toggle visibility, you need not add and remove controls, thats too much. Just set visible property once the control is added.. – nawfal Feb 02 '13 at 06:25
  • 1
    Thanks. I got it. The data i wanted within each groupbox, I made them the children of the groupbox, so hiding the groupbox would hide the content/controls within the groupbox too. – Snak3 Feb 02 '13 at 06:40