I have three forms. All forms are shown and I have a button on the third form. The button should close all the forms including the form where the button was created. Application.Exit() is not suitable for this because I don't want the application to exit including the forms that were not mentioned.

Recommended Answers

All 22 Replies

Call the forms Close() methods of both forms.
If you no longer need the forms you cold call the Dispose() methods.

Just to add to ddanbe`s post:
But you need to have both instances avaliable and forms in state "Open", otherwise will be an error.

You can set both Forms asd global and check if they are null whihle trying to close them:

//in form1:
Form2 f2;
Form3 f3:

Form1()
{
    constructor of form1
}

//open form2 method
//open form3 method

//closing methods (lets say on button click):
void buttonClose_Click()
{
    if(f2 != null)
        f2.Close();
    if(f3 != Null)
        f3.Close();
}

Mitja

if You want close two form and show the first one
try with this

private void button1_Click(object sender, EventArgs e)
Form1 frm1 = new Form1();
Form2 frm2 = new Form2();
Form3 frm3 = new Form3();
frm2.close();
frm3.close();
frm1.show();

YousafC#

Lol, what is this code? You open and close all at once. Strange. There is no point in your code.

Hello Sir Mitja I know You are the best developer and i am just new one but this code will close form2 and form3 and show Main Form.if i wrong please Amelioration.I am just new student.

I`m far from being the best devepoler, still aloooong way to go.

About you uppe code snipet:
Yes it will close forms, but when? As I can see, just after opening them. So there is no point. Maybe you only wanted to show an example how to open and close forms - then ok, but this is not the right example - examples shuld be more real life tended. Noone will open a form, and just a fraction later close it as well.
You should seperate openings and closings some how (maybe open of a button1 click, and close on button2 click). You see what I mean?

Mitja

OK ,Sir Mitja
You are right I will learn many more from your sending code,and your advise.
Thanks

You are welcome. btw, welcome to daniWeb.com!
Enjoy your staying.

Mitja

Call the forms Close() methods of both forms.
If you no longer need the forms you cold call the Dispose() methods.

How to call it? I believed that making a new instance of a form then closing it is a bad way of doing this.

Look this simple example:

//form1:
Form2 form2;
Form3 form3;
public From1()
{
    //constructor of form1
    InitializeComponent();
}

private void buttonOpenForm2(object sender, EventArgs e)
{
    if(form2 == null)
    {
        form2 = new Form2();
        form2.Show();
    }
}

private void buttonOpenForm3(object sender, EventArgs e)
{
    if(form3 == null)
    {
        form3 = new Form3();
        form3.Show();
    }
}

//one common event to close both forms (form2 and form3 - if they are opened of course):
private void buttonCloseForms(object sender, EventArgs e)
{
    //this if statements are only the check if forms are opened
    //if they are opened, they will close
    //if they are NOT opened, nothing will happen!
    if(form2 != null)
       form2.Close();
    if(form3 != null)
       form3.Close();   
}

Hope this helps
Mtija

My forms will be hidden. Are they still open?

My form1 has a button that when clicked will hide itself and show form2.

My form2 has a button that when clicked will hide itself and show form3.

My form3 has a button that when clicked will close itself, form1 and form2.

How to do this?

Ok, 1st of all, Form1 has been started with the Application.Run() method, so this form must NOT be closed in any means. If it will be, the whole application will close.

What concerns other forms, you can close then when opening a new form.
So you can do:

//on form1:

//button to open Form2:
void buttonOpenForm2_Click()
{
   Form2 f2 = new Form2();
   this.Hide();  //Form1 has to be hidden - not closed, as explained above!
   f2.Show();
}

//on form2:
//button to open Form3:
void buttonOpenForm3_Click()
{
   Form3 f3 = new Form3();
   this.Hide();
   f3.Show();   
}

//on form3:
void buttonCloseAll_Click()
{
   Application.Exit();   
}

If I use application.exit() then the entire application will close. There are other forms that were not mentioned that must not be close.

My form3 has a button that when clicked will close itself, form1 and form2.

What is that suppose to mean?
I think it means to close it self (so form3), and close form1 and form2. So close all.
If this is not it, please give me a better explanation. Be more clear about what you really want.Thx
Mitja

LOOK: Form1 CAN NOT BE CLOSED by any means! Do you got that?
You can open and close all other forms as much as you like, but not form1. As i have explained (I dont know how many time I will have to) before, when Form1 is closed, that means that whole application will close. Are we clear now?

About your third form.
Ok, when pressing button on form3, you can close form3 and form2 (do you mean Close or Hide - its a big difference?).

You can loop through Opened forms, and close them all (except Form1):

//on form3:
void buttonCloseAll_Click()
{
    foreach (Form form in Application.OpenForms)
    {
        if (form.Name != "Form1")
             form.Close();
    }
}

This code will close so far Opened forms (except Form1).
I hope we are clear now.
Mitja

Thanks Mitja for the explanations. Does the conditional if statement works with && operator? I need to keep open my two other forms(application.Run() and the other that is not mentioned) but it doesn't seem to work.

Sure it does. You can write like:

if (form.Name != "Form1" && form.Name != "Form2") //and so on!
{
    //code
}

An error occured.

Collection was modified; enumeration operation may not execute.

I see. Yes, You will have to create a list of forms which you want to close. after the loop will go through, you will loop again throug the list of opened forms. And then you will close them.

This is what I have in mind:

List<Form> list = new List<Form>();
            foreach(Form form in Application.OpenForms)
            {
                if (form.Name != "Form1")
                    list.Add(form);
            }
            for (int i = 0; i < list.Count; i++)
                list[i].Close();

Sorry for that, I completely forgot. My mistake.
Mitja

I just noticed that the opened forms is being minimized or is being overridden by other program windows

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.