Connecting Tech Pros Worldwide Forums | Help | Site Map

Trying to make a button the close the form but ask before it closes...

Newbie
 
Join Date: Oct 2007
Posts: 2
#1: Oct 26 '07
Ok I know how to make the X ask before the form closes in VB.NET. However, I am trying to make a control where a button asks 'are you sure you want to close' before it closes the form. This is what I have so far and I am doing something wrong:

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  2.         'Me.button3_click = (MessageBox.Show("Are you sure you want to close?", "Confirm Close", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No)
  3.     End Sub

Newbie
 
Join Date: Sep 2007
Posts: 20
#2: Oct 27 '07

re: Trying to make a button the close the form but ask before it closes...


You almost have it. :]

In your button's click procedure put an if statement like so:

Expand|Select|Wrap|Line Numbers
  1.         If MessageBox.Show("Do you want to close?", My.Application.Info.Title, _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> _ Windows.Forms.DialogResult.Yes Then
  2.             Me.Close()
  3.  
Basically, if the user clicks anything but yes (in this case, no) the dialog box just goes away. Otherwise the form closes.
Reply