Search code examples
.netvb.netwinformsmodal-dialogdialogresult

Has setting of DialogResult closing effect on form displayed via ShowDialog()?


After some changes, my application started to behave unexpectedly: login form displayed as

If DialogResult.OK <> New frmLogin().ShowDialog() Then ...

is automatically closing itself when line marked as 'problem is included. If I comment in out, it keeps itself open (as it worked before changes) until I call Me.Close()manually.

Private Sub Login_Click()
    If My.Application.appSession.ID > 0 Then
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    Else
        Me.DialogResult = Windows.Forms.DialogResult.Abort 'problem
        MsgBox("Invalid user name or password.", MsgBoxStyle.Critical)
    End If
End Sub

Did you observe described behavior, that purely setting value of DialogResult property closes the dialog?


Solution

  • To keep the form displayed you need to set it in this way

    Me.DialogResult = Windows.Forms.DialogResult.None
    

    Every other settings causes the form to close.

    In the form class you could read about the DialogResult property

    If the form is displayed as a dialog box, setting this property with a value from the DialogResult enumeration sets the value of the dialog box result for the form, hides the modal dialog box, and returns control to the calling form