按钮处理程序在自定义MessageBox类中不起作用

时间:2019-05-20 09:01:55

标签: vb.net

我正在为类似于消息框调用的程序制作自定义登录表单,返回DialogResult以显示登录是否成功。有三个按钮,每个按钮返回不同的DialogResult。 (还没有实现用于实际检查登录数据的代码。)但是,只有“取消”按钮实际上会触发相应的事件,其他按钮则什么也不做。

Public Class LoginBox
    Inherits Form
    Private WithEvents btnLogin As New Button With {.Text = "Login", .Size = New Size(80, 25), .Location = New Point(230, 90), .BackColor = Color.Silver}
    Private WithEvents btnCancel As New Button With {.Text = "Cancel", .Size = New Size(80, 25), .Location = New Point(230, 120), .BackColor = Color.Silver}
    Private WithEvents btnReadOnly As New Button With {.Text = "Open in" & vbCrLf & "readonly" & vbCrLf & "mode", .Size = New Size(80, 55), .Location = New Point(130, 90), .BackColor = Color.Silver}
    Private WithEvents tbNaam As New TextBox With {.Size = New Size(180, 20), .BackColor = Color.Silver, .Location = New Point(130, 20)}
    Private WithEvents tbWachtwoord As New TextBox With {.Size = New Size(180, 20), .BackColor = Color.Silver, .Location = New Point(130, 55)}
    Private lblNaam As New Label With {.Text = "Inlognaam:", .ForeColor = Color.Silver, .Location = New Point(65, 23)}
    Private lblWachtwoord As New Label With {.Text = "Wachtwoord:", .ForeColor = Color.Silver, .Location = New Point(53, 58)}
    Private Shared WaitForButton As New System.Threading.EventWaitHandle(False, Threading.EventResetMode.ManualReset)
    Private Shared antwoord As DialogResult

    Private Sub New()
        Me.FormBorderStyle = FormBorderStyle.FixedDialog
        ControlBox = False
        Text = "Inlog IC agenda"
        AcceptButton = btnLogin
        CancelButton = btnCancel
        Size = New Size(350, 205)
        BackColor = Color.DimGray
        Controls.Add(btnLogin)
        Controls.Add(btnCancel)
        Controls.Add(btnReadOnly)
        Controls.Add(tbNaam)
        Controls.Add(tbWachtwoord)
        Controls.Add(lblNaam)
        Controls.Add(lblWachtwoord)
    End Sub

    Public Overloads Shared Function Show(ByVal ShowReadOnlyButton As Boolean) As DialogResult
        Dim X As New LoginBox
        If Not ShowReadOnlyButton Then
            X.btnReadOnly.Visible = False
            X.Size = New Size(350, 180)
            X.btnCancel.Location = New Point(130, 90)
        End If
        X.CenterToParent()
        X.ShowDialog()
        WaitForButton.WaitOne()
        Return antwoord
    End Function

    Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
        'TODO: check login data, if incorrect, show error message

        antwoord = DialogResult.OK
        WaitForButton.Set()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        antwoord = DialogResult.Cancel
        WaitForButton.Set()
    End Sub

    Private Sub btnReadOnly_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReadOnly.Click
        antwoord = DialogResult.Ignore
        WaitForButton.Set()
    End Sub
End Class

测试显示“取消”按钮按预期工作。即使其他两个按钮(在我眼中)的编码方式完全相同,也无法引发适当的事件。

1 个答案:

答案 0 :(得分:0)

按钮处理程序正在运行,但是该窗体未关闭。原因是您错过了事件处理程序中的一行。

DialogResult = antwoord

因此,您的事件处理程序应类似于下面的代码所示。

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    'TODO: check login data, if incorrect, show error message

    antwoord = DialogResult.OK
    DialogResult = antwoord
    WaitForButton.Set()
End Sub

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
    antwoord = DialogResult.Cancel
    DialogResult = antwoord
    WaitForButton.Set()
End Sub

Private Sub btnReadOnly_Click(sender As Object, e As EventArgs) Handles btnReadOnly.Click
    antwoord = DialogResult.Ignore
    DialogResult = antwoord
    WaitForButton.Set()
End Sub

“取消”被视为DialogResult.None,因此您有一种印象,“取消”只能正常工作。