Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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:
 private void button1_Click_1(object sender, EventArgs e)
        {
            var frm2 = new Form2();
            frm2.Show();
            this.Close();
        }
 
Form2:
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 13-Jul-13 11:11am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 
// 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);
}
// 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.
  Permalink  
Comments
Member 8437747 at 14-Jul-13 18:20pm
   
This is a very nice and functional solution, you should try it.
bunzitop at 14-Jul-13 21:06pm
   
yo @DaveyM69, your idea is bes for memory management! i totally agree with you.
tAbdelfatah at 16-Jul-13 8:44am
   
your solution @DaveyM69 is very cool
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Form1:
 
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! Smile | :)
  Permalink  
Comments
DaveyM69 at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Of course, because you have a main form[^]. You could change visibility instead, and don't instantiate the form all the time!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 14 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100