Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing a hidden form

Tags:

c#

winforms

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.

like image 726
Arcadian Avatar asked Jun 09 '10 12:06

Arcadian


People also ask

How do you show and hide a form?

Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.

How do you display a form in C#?

Writing C# Code to Display a Modal FormPress F5 once again to build and run the application. After pressing the button in the main form to display the sub form you will find that the main form is inactive as long as the sub form is displayed. Until the sub form is dismissed this will remain the case.

How do you close a hidden form?

In order to totally close a C# application, including the hidden forms, you can use the following command in the event code of the “Exit” control: Application. Exit(); Application.

How do you hide a form in HTML?

The <input type="hidden"> defines a hidden input field. A hidden field let web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.


1 Answers

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.

like image 73
djdd87 Avatar answered Nov 01 '22 20:11

djdd87