Hey people, just a quick one here, as its getting quite annoying. Basically i want the form to only be closed when a correct password is entered using the form closing event. i have tried the following methods but they all seem to crash... any ideas?

They all start with the following initial code:

Dim strPass As String = "123" ' The default password for exit

        Dim givenPass = InputBox("Please enter password to quit", "Quit?")
If MsgBoxResult.Ok Then
            Select Case givenPass = strPass
                Case givenPass = Not strPass Or givenPass = ""
                    e.Cancel = True
            End Select

        ElseIf MsgBoxResult.Cancel Then
            e.Cancel = True

        End If
If (givenPass = "" Or givenPass = Not strPass) And MsgBoxResult.Ok Then
        '     MsgBox("Password incorrect, please try again or consult user guide")
        '     e.Cancel = True
        ' ElseIf Windows.Forms.DialogResult.Cancel = True Then
        ' ElseIf givenPass = strPass Then
        '    End
        'Else
        '    MsgBox("Unknown error, please try again")
        'End If
Try

        'Select Case MsgBoxResult.Ok And givenPass = strPass
        'Do Nothing 
        '    Case MsgBoxResult.Ok And givenPass = Not strPass Or givenPass = ""
        '  e.Cancel = True
        '      Case MsgBoxResult.Cancel
        '  e.Cancel = True
        '  End Select

        '  Catch ee As Exception
        'Throw ee
        ' End Try

any ideas? Thanks for any help in advance

i think this code is alright.

I have tried all 3, hence the commenting out, VB JIT debugger tried to handle it.

Basically ok works if the password is correct or there isnt one, if its wrong i think it crashes or if i hit cancel.

Try this:

Dim strPass As String = "123" ' The default password for exit
        Dim givenPass = InputBox("Please enter password to quit", "Quit?")
        If givenPass = strPass Then
            'do nothing.. let them leave
        ElseIf givenPass = "" Then
            'user click the cancel button
            MsgBox("You must enter a password.")
            e.Cancel = True
        Else
            'password is wrong
            MsgBox("Your password is not correct. Please try again.")
            e.Cancel = True
        End If

Hope this helps,

Johnny

Thank you Johnny, it works! I think my problem was I wasnt keeping it simple enough.. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.