Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In c# windows form, i added a button in main windows form and when i click on that button it should go to a new form window and when the new window is opened the main window should hide, ie when the button is clicked it shows only new form window. And in new form window it have a button to go back to main form window. Anyone help to code this.
Posted

First you need to create a a second form where you can pass the main form as a reference:
public partial class Form2 : Form {
private Form _parentForm;
   public Form2(Form parentForm) {
      InitializeComponent();
      _parentForm = parentForm; // store the parent form for later use
   }
}


then you can create an instance of that form on the first form:
C#
private void button1_Click(object sender, EventArgs e) {
   var form = new Form2(this);
   form.Show(); // Set it to visible
   form.Activate(); // Bring it to the front
   Hide(); // Make this form invisible
}


And to go "back" to the first form you can do something similiar on the second form.
C#
private void button1_Click(object sender, EventArgs e) {
   _parentForm.Show(); // Set it to visible
   _parentForm.Activate(); // Bring it to the front
   Dispose(); // Release the form and all of it's resources
}
 
Share this answer
 
Hope this help,


in the First form you have write the following code

C#
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
          Form2 ob = new Form2();
          ob.ShowDialog();
      this.Hide();
       }
   }


and the second form write like this.

C#
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void back_Click(object sender, EventArgs e)
        {
           Form1 ob = new Form1();
           ob.ShowDialog();
       this.Hide();
        }
    }


so this is how we navigate from one window form to another.
 
Share this answer
 
Have a look at this Stack Overflow question: "Switching between forms"[^].
 
Share this answer
 
you design two forms ,1] login 2] home

In login form use this code in login button click event

C#
this.Hide();
Home h=new Home();
h.ShowDialog();



In home form write following code in back button click event

C#
this.Close();
Login l=new Login();
l.ShowDialog();



If you want use first form data or keep that data on form then dont hide that form ,just set home form property maximized.

Reply me.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900