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.
When you do the following:
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:
Now you can just use
FormProvider.MainMenu.Show()to show the form andFormProvider.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 callFormProvider.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.
UPDATE:
Just read that
MainMenuFormis 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:You can then access the
MainMenuFormfrom anywhere in your application through theFormProviderclass.