I have a form named: form1 from which I can access two other forms: form2 and form3, with one click:
private void buttonViewEmployee_Click(object sender, EventArgs e)
{
string refNumber = 123;
Employee employee = new Employee(refNumber);
FormViewEmployee viewEmployee = new FormViewEmployee(employee);
FormViewNewUpdates newUpdates = new FormViewNewUpdates(employee);
viewEmployee.Show();
newUpdates.Show();
this.Hide();
}
Now, from form3 I would like to hide form2 and form3 and go back to form1:
private void buttonBack_Click(object sender, EventArgs e)
{
FormEditRequests editRequest = new FormEditRequests();
FormViewEmployee viewEmp = new FormViewEmployee(null);
viewEmp.Hide();
this.Hide();
editRequest.Show();
}
It seems to work but form2 actually just hides itself behind form1 but is still visible.
I tried to debug to see what happens exactly but when I debug, it works as perfect as I want but not when I am not debugging.
I tried to change the order of the code execution and also tried to use:
private void Form1_Load(object sender, EventArgs e)
{
FormViewEmployee viewEmp = new FormViewEmployee(null);
base.OnVisibleChanged(e);
viewEmp.Visible = false;
}
But didn't work either.
Can someone explains what happens during the debug mode that doesn't happen when not debugging?
CodePudding user response:
Try following code:
public static void closeAllOpenedForms()
{
FormCollection FM = Application.OpenForms;
if (FM.Count > 1)
{
for (int i = (FM.Count); i > 1; i--)
{
Form sForm = Application.OpenForms[i - 1];
sForm.Close();
}
}
}
CodePudding user response:
You can check/clone/test this code on 
FormViewEmployee, FormViewNewUpdates instances
When click in Close All button it close both instances and show FormMain instance.
2

References
