Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to Close form2 using form1?


What I have tried:

private void button_Click (object sender, EventArgs e)
if (radio1.Checked)
{
Close frm2 = new Close();
f2.Close();
}
Posted
Updated 29-May-18 6:08am

Assuming this is:

1. WinForms

2. Form1 is the main Form which created, and made visible, the
'f2 Form

3. the Button and RadioButton are on Form1
private void button_Click (object sender, EventArgs e)
{    
    if (radio1.Checked)
    {
        if(f2 != null) f2.Close();
    }
}
Should you be using a CheckBox here ? Why do you even need a Button ? The idea you check something to dispose it seems odd.

Consider if you should hide, rather than close, 'f2.

... edit ... CheckBox example
Form2 f2 = new Form2();

const string CbxShowTxt = "Show Second Form";
const string CbxHideTxt = "Hide Second Form";

private void Form1_Load(object sender, EventArgs e)
{
    checkBox1.Text = CbxShowTxt;
    checkBox1.Checked = false;
}

private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        f2.Show();
        checkBox1.Text = CbxHideTxt;
    }
    else
    {
        f2.Hide();
        checkBox1.Text = CbxShowTxt;
    }
}
... end edit ...
 
Share this answer
 
v3
Comments
Member 13675772 29-May-18 21:06pm    
My friend if only checkbox? if I checked the radio button how I will Close the form 2 using form 1?
BillWoodruff 29-May-18 22:14pm    
We can't read your mind :) Describe what you are doing in more detail: are my assumptions correct ?

Multiple RadioButtons in the same container control are used where you want the user to select only one of many.
Member 13675772 29-May-18 22:38pm    
I have a 2 monitors my friend. I'm making a display which is my form1 is my controller. What I want is When I click the button with the radio button checked or only the radio checked then the form2 will appear and my controller must still remain and then when I check the 2nd radio the form2 will disappear and my controller again must still remain... Thanks for helping my friend I appreaciated it...
BillWoodruff 29-May-18 23:05pm    
I have added an example of using a CheckBox to switch a secondary Form's visible state to the solution above.
Member 13675772 29-May-18 23:46pm    
Thanks my friend!!! It works!!! hehe.... Good job...
Try like this

Form2 obj = (Form2)Application.OpenForms["Form2"];
obj.Close();
 
Share this answer
 
Comments
Member 13675772 29-May-18 21:08pm    
My friend why it is close permanently? it should be only close once when I Click Checked the radio...
Praveen_P 30-May-18 1:53am    
Dear friend at the time when you posted the question you didn't mentioned anything about the radio button and didn't provide any code snippet you just posted "How to close form2 using form1?" and later you updated your question i posted this solution before you updating your question (may be beacause of that you downvoted my answer :) ). As my friend BillWoodruff commented i cannot read your mind :)
try this...

if (radio1.Checked == true)
{
Form1 frm = new Form1();
frm.Show();
this.Hide();
}
 
Share this answer
 
v2
Comments
Member 13675772 29-May-18 21:09pm    
The Main form which is the form1 will close my friend... I only need to closed the form 2 using form 1.

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