Link to home
Start Free TrialLog in
Avatar of Sebastion
Sebastion

asked on

Possibly very simple question - How to jump between two forms

Hello,

I've only recently begun playing around with Visual C#.net (I presume this is the place to ask this question, as I didn't see a section dedicated to this) and I have a (possibly) very simple question.

I've searched in google, and for the love of god, I just simply couldn't find how to jump between two forms using a command button.  I've only really had experience with VB, and abit of C++, but I just can't seem to find this answer.  All the tutorials that I could find seem to only use a single form, and mostly involve steps on how to put text boxes and what-not in, but none mentioned how to switch between forms.

Lets assume I have to forms:  Contacts and Suppliers.  Both forms have a command button that links to the other form.  What do I need to do, so that once pressed, a button opens the other form (and vis versa).

Thanks
Avatar of mhertzDEV
mhertzDEV

Hello:

Let say that the command button on the Contacts form is called btnSuppliers and that the suppliers form is called frmSuppliers.  This is what the code will look like.. (and vis versa)

// from the Contacts form
private void btnSuppliers_Click(object sender, System.EventArgs e)
{
      frmSuppliers gotoSuppliers = new frmSuppliers();
      gotoSuppliers.Show();
}


// from the Suppliers form
private void btnContacts_Click(object sender, System.EventArgs e)
{
      frmContacts gotoContacts = new frmContacts();
      gotoContacts.Show();
}

ASKER CERTIFIED SOLUTION
Avatar of Yurich
Yurich
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can declare a var for friend form.

      public class MyForm : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button GoToFriend;
            public   System.Windows.Forms.Form MyFriend = null;
//....
            private void GoToFriend_Click(object sender, System.EventArgs e)
            {
                  this.Hide();
                  MyFriend.Show();
            }
      }
      
      MyForm form1 = new MyForm();
      MyForm form2 = new MyForm();
      
      form1.MyFriend = form2;
      form2.MyFriend = form1;

      form1.Show();

Good luck
VINHNL
Avatar of Sebastion

ASKER

Yurich, thanks for the help, that is what I meant.