Connecting Tech Pros Worldwide Help | Site Map

CloseButton

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: 2 Weeks Ago
Hi Everybody,

How do I display a message when CloseButton on the form is clicked??
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: 2 Weeks Ago

re: CloseButton


There is an event of .FormClosing.
Attach your method to that.
  • Open the form in Designer.
  • On the Properties pallet click the events button (yellow lightening bolt)
  • Scroll down to FormClosing
  • Double-Click in the field to the right. A method stub will be created for you and an event handler will be created.
  • Put your message in that method

Expand|Select|Wrap|Line Numbers
  1.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  2.         {
  3.             System.Windows.Forms.DialogResult result = MessageBox.Show("Are you sure", "Close?", MessageBoxButtons.YesNo,
  4.                                                                        MessageBoxIcon.Question);
  5.             if (result == System.Windows.Forms.DialogResult.No) e.Cancel = true; // Cancel the close
  6.         }
  7.  
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: 2 Weeks Ago

re: CloseButton


tlhintoq

Thanks my friend it works
Reply