The form is not displayed

Perhaps this has something to do with the fact that this is mainForm, but I will ask a question. I have my mainForm, which first loads when the program loads.

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

The problem is that it displays a new shape for a second, and then closes both.

The code:

private void addFrmBtn_Click(object sender, EventArgs e) { saveForm saveform = new saveForm(); saveform.Show(); this.Close(); } 
+4
source share
5 answers

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

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

This means that your application message loop runs around the main form. Once this closes, the main thread of the user interface of the application will be with it.

You can:

Here's how you do option 3:

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

The problem is that you are closing the parent form that opens the child form. To save the form, use this.Hide (); instead of closing.

+1
source

I assume that 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(); } 
+1
source

in the project properties, shutdown mode, select "When the last form closes"

Sorry, this option seems to work only under the visual base project.

0
source

I assume that you are showing the main form with:

Application.Run (new MainForm ());

This is the default code created by visual studio and has some unforeseen actions:

 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 do not pass the form to Application.Run (), or use .Hide () instead of .Close ().

0
source

Source: https://habr.com/ru/post/1307340/


All Articles