tags:

views:

63

answers:

5
+2  Q: 

form not showing

Perhaps this has something to do with it being the mainForm, but I'll ask the question. I have my mainForm that is the first to load when the program is booted.

I then click a button called Add, which should open a new form, and close the mainForm.

The problem is, is shows the new form for a split second, then closes both.

The code:

private void addFrmBtn_Click(object sender, EventArgs e)
    {
        saveForm saveform = new saveForm();
        saveform.Show();
        this.Close();
    }
+1  A: 

The problem seems, is you are closing the parent form which opened the child form. To retain the form use this.Hide(); instead of close.

anantmf
+1  A: 

I imagine when the main form is closed, it terminates your application. Change your code to this:

private void addFrmBtn_Click(object sender, EventArgs e) 
    { 
        saveForm saveform = new saveForm(); 
        saveform.Show(); 
        this.Hide(); 
    } 
SLC
A: 

in the project properties, shutdown mode, go select "When Last Form Closes"

sorry, this option seem to only work under visual basic project

Fredou
I went to properties, which part is this under?
Luke
ha forget it, this option seem to only work under visual basic project
Fredou
A: 

I guess you are showing the Main Form with:

Application.Run(new MainForm());

This is the default code generated by visual studio and it has some possibly unexpected behaviour:

This method adds an event handler to the mainForm parameter
for the Closed event. The event handler calls ExitThread to
clean up the application.

http://msdn.microsoft.com/en-us/library/ms157902.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.run.aspx

Either don't pass the form to Application.Run() or use .Hide() instead of .Close().

dbemerlin
+3  A: 

In your Program.Main() method, you probably have something like this:

class Program
{
    void Main()
    {
        Application.Run(new MainForm());
    }
}

This means your application's message loop is running around the main form. Once that closes, the application's main UI thread goes with it.

You can either:

Here's how you do option 3:

private void addFrmBtn_Click(object sender, EventArgs e)
{
    saveForm saveform = new saveForm();
    saveform.Show();
    this.Hide();
}
Neil Barnwell