I have a winforms app with many forms. To navigate them, I want to use kind of code:
First form has this to open new window:
private void Button2_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.ShowDialog();
this.Show();
}
In the second form I have this to go back to previous window:
private void ButtonReturn_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
But I also have the function to handle closing the second form:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Sure?", "Close", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result.Equals(DialogResult.OK))
{
Environment.Exit(0);
}
else
{
e.Cancel = true;
}
}
So my question is the following: I want to use the ButtonReturn to return to previous form without triggering the form closing function, which is set to run on "FormClosing". I want it run only after pressing the red [X] in the window. The buttonReturn should navigate back to previous form without the Dialog Message showing up. How can I achieve this? Because now the closing dialog shows also after clicking on buttonReturn.