Reputation: 25
Recently I had to edit my program code so that the form will close after creating a PDF. In FormClosing()
there's a MessageBox.Show
for closing or not, depending on the DialogResult
. The problem is that when I try to Close()
, it shows me the MessageBox
, I need to close it without showing it. Thanks.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
showPDf();
// close pdf but skip MessageBox
}
Upvotes: 0
Views: 187
Reputation: 77846
You anyways want to close the form after pdf creation. So call Form's Dispose
method just after pdf creation like below and no need of registering for the OnFormClosing
event
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
showPDf();
this.Dispose();
}
Upvotes: 0
Reputation: 149518
You can use the CloseReason
property of the FormClosingEventArgs
:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.ClosingReason == CloseReason.UserClosing && MessageBox.Show("Exit?", "Exit", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
Upvotes: 1
Reputation: 1567
You can stop listening to the event like so
private void btn_PdfCreate_CloseForm_Click(object sender, EventArgs e)
{
this.FormClosing -= Form1_FormClosing
showPDf();
Close();
}
Upvotes: 2
Reputation: 82474
Use e.ClosingReason to find out if the formClosing event was fired by the user's attempt to close the form, or by something else.
for further reading, go to MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason(v=vs.110).aspx
Upvotes: 0