0

I am new to win forms (C#). The current form that I'm making has the option to create a new (blank) instance of itself, but if I ever close that first instance, all the others close as well. This is not what I want to happen. (Closing any form opened up from the first doesn't close any others, though)

I was thinking it may be because I am creating a new copy from within one of the copies/objects, so it is tied to that first object so it closes when it does; however, if I open up another form from the one opened from the first one and then close that one that was opened from the first one, the one that I opened up from it doesn't close.

I want it so that I can still close that first form without the others closing, and that when the last one closes, the program stops running.

Is there any way to do this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Risshuu
  • 101
  • 1
  • 3
  • 12

5 Answers5

4

You can run as many forms as you need but each in the separate thread

using System;
using System.Threading;
using System.Windows.Forms;

public partial class MyForm: Form
{
    public MyForm()
    {
        InitializeComponent();
    }

    private void Button1Click(object sender, EventArgs e)
    {
        var t = new Thread(() => Application.Run(new MyForm()));
        t.Start();
    }
}
Radik
  • 2,715
  • 1
  • 19
  • 24
2

You dont need to create new thread for every form. Just use this:

Form newForm1 = new Form();
this.Hide();
newForm1.ShowDialog();
this.Close();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
2

You should not have to close the Entry Point (MainForm) Form. Check the Main() method code in Program.cs code file.

public static void Main(string[] args) 
{
    // Starts the application.
    Application.Run(new Form1());
}

Read this page.

To prevent a form from closing, handle the Closing event and set the Cancel property of the CancelEventArgs passed to your event handler to true.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

Shortly:

The first form of every windows application is the main form.

If it closes, so does the application.

(See Program.cs for more details)

To solve this you can call Application.Run (While I don't know if it's smart or not)

Gilad Naaman
  • 6,390
  • 15
  • 52
  • 82
0
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //Application.Run(new Form1());

        new Form1().Show(); // any entry point with form
        Application.Run(); // without main form
    }
}

Program.cs

You can use like this code block. But when any form closed, application not finished. (Even if the last a form in application.) Because application not included mainform.

If application has not any a opened form. Application will suspend in process. You can see then the task manager. As a result you will call manually Application.Exit() method in each form close events.

mtaskopru
  • 139
  • 2