How to open a new window in windows form application?

Closing login form closing whole application? Using c# windows form.?

  • private void button1_Click(object sender, EventArgs e) { DataClasses1DataContext ddd = new DataClasses1DataContext(); var d = from c in ddd.users where textBox1.Text == c.email && textBox2.Text == c.password select c; foreach (var item in d) { if (item.email == textBox1.Text) { MessageBox.Show("correct " + item.id); Form2 f2 = new Form2(); f2.Show(); this.Close(); } } MessageBox.Show("incorrect"); } above is my code!!! can anyone explain me i'm a beginner in c#, i just want to close form1 and open form2. but it is closing whole application on closing form1 may be due to it is main form. plz tel me how to handle it, i'll be thankful if someone also tell me how can i use the data of user who enters his password in form2.

  • Answer:

    When the main form closes, the application terminates. The main form is launched from Main in Program.cs like this: Application.Run(new Form1()); When Form1 closes, the Run function will return, which means Main will exit, ending the application. One way to fix it is to hide form 1 and call ShowDialog on form2. When ShowDialog returns, call Close: Form2 f2 = new Form2(); this.Hide(); f2.ShowDialog(); this.Close();

Yawar at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

When the main form closes, the application terminates. The main form is launched from Main in Program.cs like this: Application.Run(new Form1()); When Form1 closes, the Run function will return, which means Main will exit, ending the application. One way to fix it is to hide form 1 and call ShowDialog on form2. When ShowDialog returns, call Close: Form2 f2 = new Form2(); this.Hide(); f2.ShowDialog(); this.Close();

Ratchetr

After authenticating your username and password; form f2= new form(); this.hide(); f2.Show(); Mind you, in order to close your whole program on form f2 use; Application.Exit(); Creatively you linklabel signout to call the function. Yours in code, Dasuja.

David

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.