Reputation: 13
So, in my WinForms application, I need to display several "pages" (that are groupboxes holding textbox, buttons, maskedbox and etc) on one main page on my application. Firstly I tried to use a "user controller", but it didn't work out because the name of the controllers didn't match the names that I passed to my database connection for example. So I tried to place these groupboxes one on top of another one via the "location property". All working just fine, but now I have a significant problem when I decide to make a few changes to these groupboxes controllers. It's very hard to access them and I wonder if there's another way to do so because it looks very amateur approach... How do I achieve this kind of functionality without placing groupboxes on top of another one? And I also wonder, is it correct to do it with groupbox or should I use panel?
An example of the approach:
private void btnCadastrarBeneficiario_Click(object sender, EventArgs e)
{
groupBoxUsuarioCadastro.Visible = false;
groupBoxClienteCadastro.Visible = false;
groupBoxHospitalCadastro.Visible = false;
GroupBoxMonitoramento.Visible = false;
groupBoxBeneficiarioCadastro.Visible = true;
}
I use the visibility property so I can show (or not) the groupbox and make them behave like an actual page.
---EDIT---- In order to make myself clear I created a minimal version of what I'm trying to show here:
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
groupBox2.Visible = false;
groupBox3.Visible = false;
groupBox1.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox3.Visible = true;
}
}
}
This is the code of a minimal application similar to what I'm doing. It works just fine, but I wonder if there's another way to do it, a more efficient, organized, and sophisticated way.
Upvotes: 0
Views: 359
Reputation: 54417
I would tend to do something like this:
private void button1_Click(object sender, EventArgs e)
{
HideAllBut(groupBox1);
}
private void button2_Click(object sender, EventArgs e)
{
HideAllBut(groupBox2);
}
private void HideAllBut(GroupBox groupToShow)
{
var groups = new[] {groupBox1, groupBox2};
foreach(var gb in groups)
{
gb.Visible = (gb == groupToShow);
}
}
You can obviously extend that to as many controls of whatever kind you like.
Upvotes: 2