Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have 3 forms: Form1, Form2, Form3.

When you click on buttonPlay on Form1, Form2 pops up. When you click on button1 on Form2, both Form1 and Form2 are supposed to close and Form3 will pop up.

This is my code on Form2 on the Click event of button1:

C#
private void button1_Click(object sender, EventArgs e)
        {


            Form1 x = new Form1();
            //SUPPOSEDLY hides Form1
            x.Hide();


            //Hides Form2
            this.Hide();


            Form3 f3 = new Form3();
            //Shows Form3
            f3.Show();

        }


I have tried Close() and Dispose() and using <identifier>.Visible = false; and none worked.

It only closes Form2, but I need both Form1 & Form2 to disappear. I have tried to ask for some help but we weren't able to fix it.
Thank you!
Posted
Comments
Pheonyx 27-Aug-14 4:57am    
Which is your master form? if you want to close a form, you need a reference to it. In your example code you are creating a new instance of Form1 and hiding that so that will never work.
kmllev 27-Aug-14 5:01am    
The master form is Form1. And yes, I'm trying to hide it since it works as the main menu and after the user clicks on the button on Form2 and would want to see Form3, then of course I would need to close Form1 and Form2 so the user can focus on Form3. How exactly can I reference to it? (Reference to Form1 so I can close it).

1 solution

Let codes tell you how to make this worked:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        new Form2(this).Show();
    }
}

C#
public partial class Form2 : Form
{
    Form1 f1;

    public Form2(Form1 f)
    {
        this.f1 = f;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f1.Hide();
        this.Hide();
        new Form3(f1).Show();
    }
}


C#
public partial class Form3 : Form
{
    Form1 f1;
    public Form3(Form1 f1)
    {
        this.f1 = f1;

        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f1.Close();
    }
}
 
Share this answer
 
v3
Comments
kmllev 27-Aug-14 5:10am    
THANK YOU! Although I wish you couldve explained at least a little why it's like that, but of course I don't want to be a bother. My question probably sounded too newbie/stupid but yeah I guess that's just mostly the case for beginners. Anyway, thank you again! Learned something new today. *thumbs up*
Pheonyx 27-Aug-14 5:21am    
The code provided by vikinghunter has done what I mentioned in my comment. He has customised the constructor of Form2 to accept a reference to Form1 and he stores this in a member variable. this allows form1 to be accessed by form2. He has also done the same for form3.
kmllev 27-Aug-14 5:26am    
Yes, yes, thank you! I've also looked for more examples, and I get it now. Thanks for the help!
vikinghunter 27-Aug-14 22:41pm    
In your "button1_Click()" function, you creat a new Form1 named "x". The "x" is not the Form1 who calls your Form2. So, you can not call "x.Hide()" to hide the one you want to hide indeed. This is the point. We often make this mistake.
kmllev 28-Aug-14 5:21am    
Thank you! That does make things a lot clearer. Referencing was never mentioned to us so I'm glad I came across this. Thanks!

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