Click here to Skip to main content
15,916,846 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I want to do when i close window form then ask
MessageBox.Show("Are You Sure You Want To Exit", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)

how to do it using Dispose method in designer.cs form
help me
Posted
Updated 16-Jul-12 18:50pm
v3
Comments
Trak4Net 16-Jul-12 0:05am    
Does it need to be in the Dispose method or would it be fine to use it in the form_closing event?

You should use the FormClosing event instead. Here is a simple implementation that achieves what you need:
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  if (MessageBox.Show("Are you sure you want to close the form?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
  {
            e.Cancel = true;
  }
}
 
Share this answer
 
This is how I typically handle it in the form_closing event. Not sure how you would handle it in dispose method since you won't know if user is closing or if system is logging off etc.

C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
       {
           if (e.CloseReason == CloseReason.UserClosing)
           {
              if (MessageBox.Show("Are You Sure You Want To Exit", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No)
              {
                e.Cancel = true;
              }
           }
           else
           {
               if (handler != null) handler.Stop(); //my background thread processing class
           }
       }
 
Share this answer
 
v2
this function must be returning some value...
catch it in a variable see it's content. Like Yes and now have specific numbers assgned to them , they are also referenced using vbYes and vbNo
if(var = vbYes) then
End 'Exit that is...
End if
 
Share this answer
 
Comments
kuldeepkumartak 15-Jul-12 23:42pm    
where is set this function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900