3

I have a message box that pops up when i press a close button that basicaly says" Are you sure you want to quit" but when i click the no button or cancel but the program closes any how

this is my code:

'Close Button
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click

    Dim result = MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel)
    Me.Close()

End Sub
SSS
  • 4,807
  • 1
  • 23
  • 44
Binglee2323
  • 41
  • 1
  • 2
  • 7
  • 1
    You never tested the result – John Saunders Jun 26 '13 at 02:43
  • possible duplicate of [VB.NET: MessageBox with YesNoCancel - No & Cancel triggers same event](http://stackoverflow.com/questions/2256909/vb-net-messagebox-with-yesnocancel-no-cancel-triggers-same-event) – billinkc Jun 26 '13 at 02:46

8 Answers8

5

You are doing nothing with the value of result. You need to inspect the value and determine whether you call Me.Close(). Code approximately

If result = DialogResult.Yes Then
    Me.Close()
End If
billinkc
  • 59,250
  • 9
  • 102
  • 159
4

If you are using then message box to prevent accidental form close, your approach may not work always. The message box will not be shown if the user closes the application in any other way than clicking the "Close" button.

Try using the FormClosing event.

'Close Button
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
    Me.Close()
End Sub

'FormClosing Event
Private Sub MyForm_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel) <> DialogResult.Yes
         e.Cancel = True
    End If
End Sub
bansi
  • 55,591
  • 6
  • 41
  • 52
2

You issue Me.Close() no matter what the result is. Check the result and execute Me.Close() only the user clicks Yes

Raptor
  • 53,206
  • 45
  • 230
  • 366
2
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
  If MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2, "Close application") = Windows.Forms.DialogResult.Yes Then
    Me.Close()
  End If
End Sub
SSS
  • 4,807
  • 1
  • 23
  • 44
2

Copy this:

    Dim result = MessageBox.Show(" Are you sure you want to end the Application", "School Management System", MessageBoxButtons.YesNoCancel)
    If result = DialogResult.Yes Then
        Me.Close()
    End If
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
1
 Dim result = MessageBox.Show(" Are you sure you want to quit", "System Reminder", MessageBoxButtons.YesNo)
    If result = DialogResult.Yes Then
        Me.Close()

    End If
Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47
1

If it is a child form, it opens as a result of a button in a main form:

If MessageBox.Show(" Are you sure you want to exit the application ? ", "Exit  ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then

 Me.Hide() : MainForm.Show()

        Else
            e.Cancel = True
        End If 
Josh Face
  • 107
  • 12
Ali
  • 11
  • 1
  • Please reformat your code so that it's all inside the code block, and so that it's indented properly. See [editing-help](https://stackoverflow.com/editing-help#code) for more info. – 3D1T0R May 29 '18 at 05:18
0

You can use the following code:

Dim closingfrm = MsgBox(" Are you sure to close", MsgBoxStyle.YesNo)
If closingfrm = DialogResult.Yes Then
Application.Exit()
End If
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57