How to Open a Second Form Using First Form in Windows Forms

Here I will explain how to open a second from using a first form in Windows Forms.

Here I will explain how to open a second from using a first form in Windows Forms. Before that I will explain group boxes and picture boxes in Windows Forms.

Step 1: Login form

There is a login from when I enter a password and username and then the page goes directly to another page named form2.

Login from


Step 2: Add a new form

Now go to Solution Explorer and select your project and right-click on it and select a new Windows Forms form and provide the name for it as form2.

Add a new form


And the new form is:

new form


Step 3: Coding

Now click the submit button and write the code.
 
Coding
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Data.SqlClient;  
  9. namespace First_Csharp_app  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void button1_Click(object sender, EventArgs e)  
  18.         {  
  19.             try  
  20.             {  
  21.                 if (!(usertxt.Text == string.Empty))  
  22.                 {  
  23.                     if (!(passtxt.Text == string.Empty))  
  24.                     {  
  25.                         String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";  
  26.                         String query = "select * from data where username = '" + usertxt.Text + "'and password = '" + this.passtxt.Text + "'";  
  27.                         SqlConnection con = new SqlConnection(str);  
  28.                         SqlCommand cmd = new SqlCommand(query, con);  
  29.                         SqlDataReader dbr;  
  30.                         con.Open();  
  31.                         dbr = cmd.ExecuteReader();  
  32.                         int count = 0;  
  33.                         while (dbr.Read())  
  34.                         {  
  35.                             count = count + 1;  
  36.                         }  
  37.                         if (count == 1)  
  38.                         {  
  39.                             this.hide();  
  40.                             Form2 f2 = new form2(); //this is the change, code for redirect  
  41.                             f2.ShowDialog();  
  42.                         }  
  43.                         else if (count > 1)  
  44.                         {  
  45.                             MessageBox.Show("Duplicate username and password""login page");  
  46.                         }  
  47.                         else  
  48.                         {  
  49.                             MessageBox.Show(" username and password incorrect""login page");  
  50.                         }  
  51.                     }  
  52.                     else  
  53.                     {  
  54.                         MessageBox.Show(" password empty""login page");  
  55.                     }  
  56.                 }  
  57.                 else  
  58.                 {  
  59.                     MessageBox.Show(" username empty""login page");  
  60.                 }  
  61.                 // con.Close();  
  62.             }  
  63.             catch (Exception es)  
  64.             {  
  65.                 MessageBox.Show(es.Message);  
  66.             }  
  67.         }  
  68.     }  

Step 4: Output

When you click on the submit button a new form will be opened named form2.