I was wondering guys how can I close two Forms From with its Form_Closing Eventhandler.
Example:
MainForm;
MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
var d = (MessageBox.Show("Exit Program","Confirm",MessageBoxButton.YesNo,MessageBoxIcon.Question);
if(d== DialogResult.Yes)
{
e.cancel=false;
}
else
{
e.cancel=true;
}
}
In Another Form CAlled LoginForm;
LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
var f = (MainForm)Application.OpenForms["MainForm"];
if(f!=null)
{
if(f==DialogResult.Yes)
Application.Exit();
}
}
My Problem is How Do I call e.cancel function in the MainForm so that I could Override the FormClosing e.cancel=false and Close the Application with Application.Exit(); From LoginForm
LoginForm is a Modal Dialog and its parent is MainForm.
After I have read your comment I suggest using a different approach for your problem.
Use the
DialogResultproperty of the login form to indicate if the app should be terminated or not, Here is an example, this code should run on the MainForm.Note: It is a basic example that probably will need some modification according to your project, for example checking if there is some long process that runs and the form should be delayed and invoked after the process finished...
please read comments inside the example: