Click here to Skip to main content
15,888,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have developed a Windows Form Application using C#.
It has three different forms.
Now I want to redirect from the first form to the second and from the second to the third on Button_Click event,
I have tried both the below mentioned methods.
But, both are not working.
Please help me.

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
{
      Form2 obj = new Form2();
      if (obj == null)
      {
          obj.Parent = this;
      }
      obj.Show();
      this.Hide();
}


I have tried both above and below methods

C#
private void button2_Click(object Sender, EventArgs e)
{
     this.close();
     form2 f2 = new form2();
     f2.show();
}
Posted
Updated 30-May-16 1:14am
v3

Every time you create a new Form inside a Method, the "life-time" of that Form is limited to the scope of the Method. So, the new Forms you create inside the two Methods you show here essentially "don't exist" outside those Methods.

Assuming you have a "Main Form" (that you are using the default Windows Form App that Visual Studio creates):

1. create re-usable instances of your other two Forms in the Main Form's Class scope:
C#
Form2 form2Instance = new Form2();

Form3 form3Instance = new Form3();
and make visible (using 'Show) as necessary.

What if you want to have a Button on these "secondary" Forms that takes you back to the Main Form ? Expose a Public Action delegate in the secondary Forms:
C#
// in Forms 2 and 3

public Action<form> GetBackHome { set; get; }</form>
Then, when the user clicks a Button in Forms 2, or 3 named 'GoBackToMain:
C#
private void GoBackToMain_Click(object sender, EventArgs e)
{
     if(GetBackHom != null) GetBackHome(this);
}
Then, in the Main Form, you can insert a "return from" delegate instance into Forms 2, and 3:
C#
form2Instance.GetBackHome += returnedFromSecondaryForms;

private void ReturnedFromSecondaryForms(Form theForm)
{
    Console.WriteLine("returned from Form: {0}", theForm.Name);

    // return Focus to the Main Form
    this.Focus();

   // do whatever you want to with the Form you returned from
   // Hide it ?
   theForm.Hide();
}
Note:

1. think carefully about when you want to 'Hide a Form, rather than 'Close it. Use Hide(); when you want to keep the Form "alive," re-use it. Use "Close(); when you want to delete the reference to the Form (the reference will then be garbage-collected automatically unless you've done something weird).
 
Share this answer
 
Comments
Member 11764921 31-May-16 2:22am    
BillWoodruff
ZurdoDev 31-May-16 9:23am    
+5. Great explanation.
BillWoodruff 31-May-16 9:39am    
Thanks, Ryan !
First of all, "Page" is used for WebForms. However, if you mean "Form", pay visit to the the link below.

stackoverflow.com: How can I navigate between forms
 
Share this answer
 
v3
Comments
Member 11764921 31-May-16 2:21am    
thanks shahin
Shahin Khorshidnia 31-May-16 7:25am    
Cheers

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