0

well, i did a multi forms app in c#. This means that i had more forms including one which is the main menu. All forms has ,among others,a button which return to the main menu.(Except from the main menu form which has buttons which access a specific form) Well this is how i make this transition.

this.Close();
Form1 Myform1 = new Form1()
Myform1.Show();

The problem is that when i randomly close the app, it dissapear , as i want,but it doesn't close completely.I mean it only dissappeared, but it still runs. WHY? and HOW TO DO IT TO CLOSE ENTIRELY ?

Lazu Razvan
  • 49
  • 2
  • 8

1 Answers1

0

The basic problem you are running into is that by default, your Winforms program will exit when the main form (the one that is displayed first) is closed. This is because the reference to this form is passed to the Application.Run(Form) method in the program's Main() method.

There are a variety of ways to deal with the issue, but IMHO the most straightforward is to not close the form. Instead, call the Hide() method on your main form. This will cause it simply to not be shown, but not closed. When you want to return to it, just call Show() on that form instance and it will be visible again.

Naturally, to accomplish this you will need to pass that reference to any other code that may want to use it. So you may wind up for example with code that looks like this:

this.Hide(); // Not Close()!
Form1 Myform1 = new Form1(this)
Myform1.Show();

Then somewhere in the Form1 class, where you are ready to close that and show the main form again, you would call the Show() method. That might look something like this:

partial class Form1 : Form
{
    private readonly MainForm _mainForm;

    public Form1(MainForm mainForm)
    {
        _mainForm = mainForm;
    }

    protected override void OnFormClosed(FormClosedEventArgs e)
    {
        _mainForm.Show();
    }
}

Another way to handle this is to just not have the MainForm passed to the Application.Run() method in the first place. Instead, call the parameterless overload of Application.Run(). But then you will need some other mechanism for closing the program. For example, calling Application.Exit() at the appropriate time.


While this seems to me to very a fairly common question, I was unable to find another question that seemed like an exact duplicate. However, there are definitely a large number of other related questions, including the following:

C# Application.Run without Form
Close a windows form without exiting the entire application
How can I close a login form and show the main form without my application closing?
If I close one of my forms, they all close
Methods this.hide() vs this.close()

Those last two are particularly relevant, but unfortunately the top answer in each is probably the worst way to solve the problem (i.e. put each form into its own thread…one should use multiple UI threads as a last resort only, and doing so is definitely not called for here). So take any advice you read in any of the above questions with a grain of salt. There is some good information among the answers, but there is also a fair amount of bad advice as well.

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136