Binglee2323
Binglee2323

Reputation: 41

Confirming program close with a messagebox

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

Upvotes: 3

Views: 51849

Answers (8)

Ali
Ali

Reputation: 11

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 

Upvotes: 1

Yakub Ahmed Yakub
Yakub Ahmed Yakub

Reputation: 1

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

Upvotes: 0

Johannes Kawali
Johannes Kawali

Reputation: 29

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

Upvotes: 2

Dr. Ugs Production
Dr. Ugs Production

Reputation: 11

 Dim result = MessageBox.Show(" Are you sure you want to quit", "System Reminder", MessageBoxButtons.YesNo)
    If result = DialogResult.Yes Then
        Me.Close()

    End If

Upvotes: 1

bansi
bansi

Reputation: 56982

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

Upvotes: 4

SSS
SSS

Reputation: 5403

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

Upvotes: 2

billinkc
billinkc

Reputation: 61211

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

Upvotes: 5

Raptor
Raptor

Reputation: 54212

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

Upvotes: 2

Related Questions