VB2010Express MessageBox多次出现

时间:2016-06-30 21:09:50

标签: vb.net visual-studio-2010

初学者在这里使用VB2010 Express - 使用MessageBox.show IF / ELSEIF语句但我的按钮必须被按下几次,第一次btn一次,第二次,第二次,第三次三次,然后生成的消息对话框实际显示。我不知道我的Dim语句如何与此相关联。昏暗的结果......

    Private Sub btnMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMessage.Click
        Dim Result As 
        If MessageBox.Show("Click something.", "Title", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Abort Then
            MessageBox.Show("Aborted")
        ElseIf MessageBox.Show("Click something", "Title", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Retry Then
            MessageBox.Show("Retrying.")
        ElseIf MessageBox.Show("Click something", " Title", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Ignore Then
            MessageBox.Show("Ignoring.")
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

您缺少一些非常重要的非常基本的编程概念。这不是教你那些基础知识的地方 - 这就是大学的用途。

足以说你有三个完全独立的消息框,因此它们可以出现三次。

解决方案是(正确)使用变量:

Dim result
result = MessageBox.Show("Click something.", "Title", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question)
'store the chosen answer in the "result" variable, then use it to check the result

If result = Windows.Forms.DialogResult.Abort Then
    MessageBox.Show ("Aborted")
ElseIf result = Windows.Forms.DialogResult.Retry Then
    MessageBox.Show ("Retrying.")
ElseIf result = Windows.Forms.DialogResult.Ignore Then
    MessageBox.Show ("Ignoring.")
End If
相关问题