Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to create a Win form with multiple pages?

My goal
I am working on a project in C# using Visual Studio 2013. The project is one that I intend to contain a lot of pages. These pages are all linked together using buttons. My problem is that I cannot come up with an efficient and elegant solution for this.

My attempts
So far I have came up with two potenial solutions to my problem. First I added extra forms and then on button press I hid the current form and displayed the new form, like so:

Form2 frm = new Form2();
frm.Show();`
Form1.Hide();

While this does work, I have two problems with it.

  1. My project will end up with hundreds of forms
  2. The transition between forms looks sloppy. I am aiming for a browser like transition by where all navigation occurs on one window, without opening and closing others.

The second potential solution I tried incorporated the use of Panels. So I essentially created each page on a different Panel. Then the appropriate panel was shown upon a button press and the rest were hidden. Like this:

private void button1_Click(object sender, EventArgs e)
{
    mainMenuPanel.Hide();
    submenuPanel1.Show();
    submenuPanel2.Hide();  
    submenuPanel3.Hide();   
    submenuPanel4.Hide();     
}

This is exactly what I was looking for however my issue with it is that managing the vast amount of panels quickly became a nightmare. Editing the controls on a Panel that was hidden behind 9 other Panels and as the number of panels in my project was only going to grow - this does not seem like the ideal solution in its current form.

In my head I thought there maybe an option in Visual Studio 2013 that allows me to 'hide' the Panels I am not using on the form, or drag them off the form temporarily. Is that an option in Visual Studio.

If not do any of you know a more efficient and manageable way of achieving this?

Thanks in advance.

like image 365
zxnked Avatar asked Oct 30 '25 17:10

zxnked


2 Answers

If you are stuck using WinForms, your best bet is probably using UserControls. you can actually extend the UserControl class out to be a "page" ie: UserControlPage. This makes the form much simpler in function, but you will need to do some finicky work with handling events /passing data if the controls need to talk to each other.

if you aren't nailed into using Winforms, WPF supports all of this natively, and has wonderful tools for building all the pages you would need, and storing/populating your data, and propagating events.

like image 180
beteez Avatar answered Nov 02 '25 07:11

beteez


If you want to have single form with changing content, and you don't want to mess up with panels in one form, then solution is user controls. You will be able to create them dynamically and add to form controls. Also there is no mess, because your form will be very simple - you can have single 'placeholder' control which will be used to dock user control which is currently displayed (e.g. panel control):

private void ShowContent(Control content)
{
    placeHolderPanel.Controls.Clear(); // clear current content
    placeHolderPanel.Controls.Add(content); // add new
    content.Dock = DockStyle.Fill; // fill placeholder area
}

Usage:

private void button1_Click(object sender, EventArgs e)
{
    ShowContent(new FooUserControl());
}
like image 33
Sergey Berezovskiy Avatar answered Nov 02 '25 09:11

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!