views:

48

answers:

1

Hi, I am making simple graphical game in WinForms and currently I would like to have a menu displayed when the game starts. My only problem is that I am not sure regarding the structure of the application itself, this is what I have so far:

1) To have a Menu form and Game form. When New game is selected, create Game form and dock it to Menu form - I cannot dispose Menu form as the app would exit. Or can I switch the messageloop to different form? I doubt so
2) To have some main form that enables me to create and dispose both menu and game forms 3) Entirely different way?

+1  A: 

One way I'm using frequently when developing applications is to use the main form as a container for views (a view being a UserControl), containing a panel to add the views to.

What you could do is:

  1. Define two user controls, one for the menu, one for the game's logic. The menu view should expose one event for each menu item that the main form can attach to.
  2. When starting, show the menu control. When the event for "Play" is invoked, the main form should switch to the game view.

The following pseudo-code will help you switch views:

private void SwitchView(Panel container, UserControl newView)
{
    if (container.Controls.Count > 0)
    {
        UserControl oldView = container.Controls[0] as UserControl;
        container.Controls.Remove(0);
        oldView.Dispose();
    }

    if (newView != null)
    {
        newView.Dock = Dock.Fill;

        // Attach events
        if (newView is ...)
        {
           ...
        }

        container.Controls.Add(newView);
    }
}

Please note that this code may not compile properly, I writing this from my head. But it will give you a general idea of this can be done.

Thorsten Dittmar