Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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
0
Sebastion
Asked:
Sebastion
1 Solution
 
mhertzDEVCommented:
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();
}

0
 
YurichCommented:
i'm afraid it's not exactly right and i'm not sure it's going to work,
since if I click 10 times on any of the button I'm going to have 10 instances of the very two classes?
why would you want to create new instances including of your main form as well...

few things are not clear - do you want to hide another form while you're working with one or it still can be sitting somewhere behind the first one?

are they only two forms you have and is your first one, Contacts, your main form as well?

let's presume you first form is your main form, you want to create your secondary form and then switch between them on will, view only the one your'r working with...

1. Form1 and Form2, Form1 is the main form, Form2 is added form.
Form1 has a button "Go to Form2", and Form2 has a button "Go to Form1"

2. create an instance of your second form somewhere in the class scope of your first form (can do before the constructor):

Form2 f2 = null;

// and here your constructor will be...

3. Add a button, name it whatever your want and double-click it to get to its click event, add the code and it should look like:


private void button1_Click(object sender, System.EventArgs e)
{
   if( f2 == null )
   {
      f2 = new Form2();
      f2.Owner = this;
      this.Hide();
     f2.Show();      
}
   else
   {
       this.Hide();
       f2.Show();
   }
}

4. add a button to your second form, double click it, add the code below, and your click_event should look like:

private void button1_Click(object sender, System.EventArgs e)
{
   this.Hide();
   (( Form )Owner ).Show();      
}

that should work fine

regards



0
 
vinhnlCommented:
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
0
 
SebastionAuthor Commented:
Yurich, thanks for the help, that is what I meant.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now