Click here to Skip to main content
15,886,105 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three form in C#, main form is A, second is B & third is C. I want to close B&C both form (if B&C are activated) from main form using a button.
Posted
Updated 7-Dec-13 12:46pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Dec-13 18:48pm    
System.Windows.Forms? Tag it.
—SA

The "requirement" is self-contradictory: as soon as you say "from main form using a button", and main form is A, it means that the forms B and C are not activated.

Anyway, what's wrong with System.Windows.Forms.Form.Close? However, closing forms (not modal ones, which is your case) is often not recommended, as you won't be able to use them again, in such cases, it's better to hide them, to be able to show them later:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.hide(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.show(v=vs.110).aspx[^].

—SA
 
Share this answer
 
Assuming that your Main Form code has created instances of Forms B, and C like this:
C#
B FormBInstance = new B();
C FormCInstance = new C();
And the Forms have been shown like this:
C#
private void MainForm_Load(object sender, EventArgs e)
{
    FormBInstance.Show();
    FormCInstance.Show();
}
Then the Button Click EventHandler might look like this:
C#
private void CloseBC_Click(object sender, EventArgs e)
{
    if (FormBInstance != null) FormBInstance.Close();
    if (FormCInstance != null) FormCInstance.Close();
    CloseBC.Enabled = false;
}
I think it's a good idea that if some user-interface Control no longer "does" anything it should be disabled ... or hidden.

But, do keep in mind that once you "close" any instance of a Form: all data, all the results of user interactions with the Controls on the Form, are: lost.
 
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