hunter isaacs
hunter isaacs

Reputation: 1

In Visual Basic forms confirm exit using message box

I have a basic me.close() button. I need to add a second step confirmation if it is pressed using a message box pop up.

Upvotes: 0

Views: 2845

Answers (2)

Vector
Vector

Reputation: 3235

Due to the vagueness of this question as mentioned in the above comment and after reading numerous post about how to manage the titlebar X Close Button I thought I would expand on the above answer because I could not enter the line code e.Cancel = True
My problem was I could use this code on a form to navigate back to another form no problem
But when I wanted to ignore navigation and just leave the calling form showing nothing seemed to work the one line of code to note here is result = ""
While reading I also discovered code that would inactivate the X Close Button on the titlebar
As a BONUS I have included a KeyPress Event code that preforms Application.Exit

TitleBar X Close Button Code Below

    Private Sub frmOne_Closing(sender As Object, e As EventArgs) Handles Me.Closing

    Const message As String = "YES Exit Progtam" + vbCrLf + vbNewLine + "NO CANCEL"
    Const caption As String = "EXIT"

    Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If result = DialogResult.Yes Then
        Application.Exit()
    ElseIf result = DialogResult.No Then
        txtBoxOne.Text = "CANCEL"
        result = ""
        'Me.Hide() This Code Was Used On frmThree 
        'frmOne.Show()'To Navigate Back to frmOne
    End If
End Sub

KeyPress Event Code Below

    Public Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
    'frmOne Property KeyPreview needs to be set to True
    If Asc(e.KeyChar) = 27 Then
        'MessageBox.Show("You Pressed " & e.KeyChar)
        Application.Exit()
    End If
End Sub

Code to Inactivate the X Close Button

    'Private Const CP_NOCLOSE_BUTTON As Integer = &H200
'Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
'    Get
'        Dim myCp As CreateParams = MyBase.CreateParams
'        myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
'        Return myCp
'    End Get
'    'This works but NOT what I want
'End Property

Upvotes: 1

David
David

Reputation: 6131

In your Form's closing event, you should initialize a MessageBox and check if the result from the dialog is not yes. If so, then cancel the event. Something along the lines of:

Public Sub MyForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyForm.FormClosing
    If MessageBox.Show("Do you want to close the form?", "Confirm", MessageBoxButtons.YesNo) <> DialogResult.Yes Then
        e.Cancel = True
    End If
End Sub

I would have thought that there is already an example somewhere, and as it turns out there already is on MSDN: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.closing?view=netframework-4.8#examples

When in doubt, try to search the MSDN documentation. This is where I personally get 99% of my information.

Upvotes: 1

Related Questions