Ipwnusck Ipwnusck - 1 year ago 102
C# Question

My WinForm closes itself immediately after it is shown

After I show my WinForm, it closes immediately after. I have three WinForms: my starting one, a progress bar, and then my main one. On the starting WinForm, the user clicks a button which leads them to the progress bar. Then once the progress is completed, it runs the main form through a function in another class, but then it closes immediately. It looks something like this:

//Starting WinForm
private void button1_Click(object sender, EventArgs e)
ProgressBarForm PBF = new ProgressBarForm();
//Progress Bar Form stuff
private void ProgressBarForm_Shown(object sender, EventArgs e)
// Stuff for progress bar to load
progressBar1.Maximum = 100;
if (progressBar1.Value == 100)
// In main class
public static void ExecuteMain()
MainForm MF = new MainForm();

// Other stuff that leads to another function

And right there it closes immediately. I've tried running a separate thread for it, but it still doesn't work. I've tried keeping it in a while loop, but it constantly keeps loading and doesn't allow any interaction. Also, if this counts, I'm trying to draw on the form aswell. My question is, why does the WinForm close immediately while the other two do not?

Kye Kye
Answer Source

Change PBF.Show(); to PBF.ShowDialog();

Your closing the window straight away - that's why it closes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download