Showing a hidden form

Practically This works for me....

public class MainWindow : Form
{
    Form _mainMenuForm = new MainMenuForm();
}

calling it through a button click event.

private void buttonclick()
{
    if (_mainMenuForm.Visible)
    {
         _mainMenuForm.Visible = false;
    }
    else
    {
         _mainMenuForm.Visible = true;
    }
}

The simplest and easiest way is to use LINQ and look into the Application.OpenForms property. I'm assuming you have only 1 instance of the form (hopefully!), otherwise make sure to have to have some public property on the hidden form to be able to differentiate it.

The following code will un-hide the form for you:

var formToShow = Application.OpenForms.Cast<Form>()
    .FirstOrDefault(c => c is MainMenuForm);
if (formToShow != null)
{
    formToShow.Show();
}

You need to keep a reference to the first form when it's created and then the code that holds that reference can call Show on it.

If you don't open that form from somewhere but it's set as the startup form, then you either need to change it so that you have a Main method that opens that form or you can have that form store a reference to itself somewhere that can be accessed from other places.

For example, an quick and ugly way would be to, add a public static property to your mainform and then when you hide the form it also writes this to that property which can then be retrieved when needed by other parts of the code.


When you do the following:

MainMenuForm frmMainMenu = new MainMenuForm();
frmMainMenu.Show();

You are creating and showing a new instance of the MainMenuForm.

In order to show and hide an instance of the MainMenuForm you'll need to hold a reference to it. I.e. when I do compact framework apps, I have a static classes using the singleton pattern to ensure I only ever have one instance of a form at run time:

public class FormProvider
{
   public static MainMenuForm MainMenu
   {
       get
       {
          if (_mainMenu == null)
          {
            _mainMenu = new MainMenuForm();
          }
          return _mainMenu;
       }
   }
   private static MainMenuForm _mainMenu;
}

Now you can just use FormProvider.MainMenu.Show() to show the form and FormProvider.MainMenu.Hide() to hide the form.

The Singleton Pattern (thanks to Lazarus for the link) is a good way of managing forms in WinForms applications because it means you only create the form instance once. The first time the form is accessed through its respective property, the form is instantiated and stored in a private variable.

For example, the first time you use FormProvider.MainMenu, the private variable _mainMenu is instantiated. Any subsequent times you call FormProvider.MainMenu, _mainMenu is returned straight away without being instantiated again.

However, you don't have to store all your form classes in a static instance. You can just have the form as a property on the form that's controlling the MainMenu.

public partial class YourMainForm : Form
{
   private MainMenuForm _mainMenu = new MainMenuForm();

   protected void ShowForm()
   {
      _mainMenu.Show();
   }

   protected void HideForm()
   {
      _mainMenu.Hide();
   }
}

UPDATE:

Just read that MainMenuForm is your startup form. Implement a class similar to my singleton example above, and then change your code to the following in the Program.cs file of your application:

Application.Run(FormProvider.MainMenu);

You can then access the MainMenuForm from anywhere in your application through the FormProvider class.

Tags:

C#

Winforms