3

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.

zxnked
  • 35
  • 1
  • 4
  • Did you try with a "ViewManager" class (simply a container form) with a single NavigateTo(Control control) method? It'll replace its content with given one. Each "page" is a Control (or a UserControl) and you'll navigate between them – Adriano Repetti Mar 13 '14 at 17:15
  • [ViewManager](http://msdn.microsoft.com/en-us/library/microsoft.windows.design.documents.viewmanager(v=vs.90).aspx) - MSDN says _This API supports the .NET Framework infrastructure and is not intended to be used directly from your code_ Are you talking about that one linked? – Steve Mar 13 '14 at 17:18
  • Just in case you're interested: XAML-based modern UI frameworks (which replace winforms) such as WPF have all this functionality built-in. No need to reinvent the wheel. BTW winforms is not recommended for any new projects, only to maintain legacy applications. – Federico Berasategui Mar 13 '14 at 17:18
  • do you need so many pages for user input or to display static content? – giammin Mar 13 '14 at 17:27
  • @giammin - They are displaying static content – zxnked Mar 13 '14 at 17:49
  • 1
    @HighCore Thanks for that. I was not aware that Winforms were becoming obsolete for new projects. I will try my project again on WPF and see how it goes. – zxnked Mar 13 '14 at 17:50
  • @zxnked great! Keep in mind that [**The WPF Mentality**](http://stackoverflow.com/a/15684569/643085) is very different from the traditional winforms approach. You'll learn as you go, but I suggest you start reading on that link and related articles. – Federico Berasategui Mar 13 '14 at 17:54

3 Answers3

3

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.

beteez
  • 46
  • 1
  • Thank you. Several others have suggested WPF. I was not aware they were so powerful when building forms. I have decided to start again with WPF. – zxnked Mar 13 '14 at 17:52
  • @zxnked WPF is certainly better equipped to handle this scenario, but be aware that the learning curve is quite a bit steeper than Windows Forms' – Luc Morin Mar 13 '14 at 18:06
2

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());
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

You could subclass the Panel class and create as many of those custom panels as needed, then they would be inserted on your Main Form, and managed as you described.

The advantage is that you would be able to individually edit them as a separate user control.

The drawback is that you lose direct event handling of controls on those panels from the main form. You can still define your own events on those panels and delegate the individual control events.

There's always a trade-off somewhere.

Cheers

Luc Morin
  • 5,302
  • 20
  • 39