Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,
I am new to windows application. I have two forms and i want to navigate between these forms. When I Click button on form1, it moves to form2. The problem is when i click on button on form2 to navigate to form1, it closes the application. I am using the following code

Form1:
C#
private void button1_Click_1(object sender, EventArgs e)
       {
           var frm2 = new Form2();
           frm2.Show();
           this.Close();
       }


Form2:
C#
private void button2_Click(object sender, EventArgs e)
        {
            
            var frm1= new Form1();           
            frm1.Show();
            this.Close();
        }


I Shall be thankful for your help.
Regards
Usama
Posted

Using the code you have been given so far you will be creating a new instance of Form1 each time the button in Form2 is clicked, which is probably not what you are wanting.

To show your original Form1 you should create an event in Form2 that your hidden Form1 instance subscribes to, so it can show itself:

C#
// In Form2
public event EventHandler ButtonClicked;

protected virtual void OnButtonClicked(EventArgs e)
{
    EventHandler eh = ButtonClicked;
    if (eh != null)
        eh(this, e);
}
private void button1_Click(object sender, EventArgs e)
{
    OnButtonClicked(e);
}

C#
// In Form1
private void button1_Click(object sender, EventArgs e)
{
    // A new Form2 instance. Cache this in a field and only create if null if you want the same Form2 instance each time
    Form2 form2 = new Form2();
    form2.ButtonClicked += new EventHandler(form2_ButtonClicked);
    form2.Show();
    Hide();
}
private void form2_ButtonClicked(object sender, EventArgs e)
{
    Form form2 = sender as Form;
    if (form2 != null)
    {
        form2.Close();
        Show();
    }
}


Another way is Constructor Injection into Form2 so it receives the Form1 instance, but this is rarely a good idea as it couples Form2 to Form1.
 
Share this answer
 
Comments
Member 8437747 14-Jul-13 18:20pm    
This is a very nice and functional solution, you should try it.
bunzitop 14-Jul-13 21:06pm    
yo @DaveyM69, your idea is bes for memory management! i totally agree with you.
tAbdelfatah 16-Jul-13 8:44am    
your solution @DaveyM69 is very cool
Form1:

C#
private void button1_Click_1(object sender, EventArgs e)
        {
            var frm2 = new Form2();
            frm2.Show();
            this.Hide()
        }

:

Form2
private void button2_Click(object sender, EventArgs e)
        {
            
            var frm1= new Form1();           
            frm1.Show();
            this.Close();
        }

Hope this will help you! :)
 
Share this answer
 
Comments
DaveyM69 14-Jul-13 16:17pm    
You are creating a new Form1 every time the Button is clicked in Form2. The OP hasn't stated whether this is what he needs, but normally the initial instance is required.
Additionally, these instances are only ever hidden and never closed - that's going to start using a lot of unnecessary memory very quickly!
Of course, because you have a main form[^]. You could change visibility instead, and don't instantiate the form all the time!
 
Share this answer
 

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