Hi, I want to close a form, when another form closes. Basically, I have a search form that opens up a results form, which contains a dataGridView only. when the user closes the results form, I want the search form to close also. Is this even possible?
Thanks

Yes, you can do that. Somewhere you open the search form, and then you open the results form. Can you post those code areas?

Do you want to close the application, or are other forms open?

Hi, this is the code for the search btn that opens up the results form:

private void btnSearch_Click(object sender, EventArgs e)
        {
            criteria1 = cbCriteria1.Text;
            criteria2 = cbCriteria2.Text;
            //criteria3 = cbcriteria3.Text;
            search1 = txtCriteria1.Text;
            search2 = txtCriteria2.Text;
            //search3 = txtCriteria3.Text;
            if (criteria1 == "" && criteria2 == "" &&  search1 == "" && search2 == "" )
            {
                MessageBox.Show("You have not entered any search criteria or paramaters. Your search cannot be started","Enter Search Criteria!");
            }
            else
            {
                SearchResultsFrm frm = new SearchResultsFrm(criteria1, search1, criteria2, search2);
                frm.Show();
            }

        }

Opening the search form is basic stuff:

private void tsBtnFind_Click(object sender, EventArgs e)
        {
            FindFrm frm = new FindFrm();
            frm.Show();
        }

Thanks

It is only these 2 form that Iwant to close. the rest of the application is still running

I'm now trying to call a button's event handler from the first form, to close that form while second form is closing (in the On_Closing Event)

private void On_Closing(object sender, FormClosingEventArgs e)
        {
            FindFrm frm = new FindFrm();
            frm.button1_Click(sender, e);
        }

This does nothing. It probably has something to do with the object sender, but I don't know what to do???

can't paste my code here, no internet access from my laptop, but I found a solution: you open a reference to the form you want to close like this:

FindFrm frm = (FindForm).Application.OpenForms["FindFrm"];
frm.Close();

This code went into the formClosing event handler. Now when the user closes the second form, both forms close.

Now if only someone will explain to me what I just did. (I need to learn, not just copy)
thanks

This is not a good way to close the form. You better do a loop of all forms, and then check which you want to select. Selected put into an array (pre-created), and when the code goes throught the loop (when it ends), go through the array of forms, and close them.
Array has to be created and put the forms inside, because you are not allows to change the state of the forms (in your case closing them) while in foreach loop. You will get an error.
Hope it helps explaing how to close opened forms.

why is:

FindFrm frm = (FindFrm).Application.OpenForms["FindFrm"];
frm.Close();

so different from:

FindFrm frm = new FindFrm();
frm.Close();

I know the second one only creates an instance of FindFrm, but what does the first one do?

Heres an example:

List<Form> list = new List<Form>();
            foreach (Form f in Application.OpenForms)
                if (f.Name == "Form2")
                    list.Add(f);

            //close seleced (in this case only FindFrm will be closed:
            foreach (Form f in list)
                f.Close();

Maybe some forms do not have set the NAME propety. So you will have to set it manually in some place of form creation. Best it to put the code into "FormName.Designer" class:

//where is this code:
this.Text = "FindFrm";
//put this code too:
this.Name = "FindFrm";

If I understand correctly, the code find the actual form as opposed to only creating an instance, therefor Close() method will work. thanks for the help. hopefully one day I can help other beginners

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.