Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do i show a from that have been hidden using

this.Hide();

I have tried

MainMenuForm.Show();

and this just says i need an object ref. I then tried:

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

Which seems to show the appropriate form. But when you exit the app, it is still held in memory because it hasn't shown the form that was hidden, instead it has shown a new version of the form. In effect having 2 instances of the form (one hidden, one visible).

Just to clarify, the MainMenuForm is the startup form. When (for example) Option 1 is clicked, the MainMenuForm then hides itself while opening up the Option 1 form. What i would like to know is how to i make the Option 1 form that the MainMenuForm opens "unhide" the MainMenuForm and then close itself.

What's the correct procedure here?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
174 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...