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?
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.
List<Form> list = new List<Form>();
foreach (Form f in Application.OpenForms)
if (f.Name == "Form2")
//close seleced (in this case only FindFrm will be closed:
foreach (Form f in list)
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";