0

我正在浏览 200 个关于 VB 的短视频教程,在处理消息框的教程中,讲师要求我们尝试用一些 Else If 语句来响应更多按钮。我让它工作了,有点,除了我必须点击两次重试按钮才能工作,我必须点击忽略按钮三次才能工作。有人可以告诉我我做错了什么吗?我的代码如下。

Private Sub btnShowMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowMsg.Click

    If MessageBox.Show("Click Something", "Make a Selection", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Abort Then
        MessageBox.Show("Hey, you Aborted me!")

    ElseIf MessageBox.Show("Click Something", "Make a Selection", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Retry Then
        MessageBox.Show("You are trying to retry, aren't you?")

    ElseIf MessageBox.Show("Click Something", "Make a Selection", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Ignore Then
        MessageBox.Show("Don't you dare ignore me!!")

    End If
End Sub
4

3 回答 3

0

在最坏的情况下,您将创建三个消息框(每个If/子句上一个),而您应该只创建一个消息框。ElseIf

先存储消息框的结果:

' create only one MessageBox and store the result '
Dim result = MessageBox.Show("Click Something", "Make a Selection", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question)

If result = Windows.Forms.DialogResult.Abort Then
    ...
ElseIf result = Windows.Forms.DialogResult.Retry Then
    ...
ElseIf ...
   ...
于 2013-10-01T12:48:52.433 回答
0

那是因为您在每个 IF 上都在问这个问题。您需要预先询问一次并存储结果值,或者您可以做一个案例陈述。

Dim result As Windows.Forms.DialogResult = MessageBox.Show("Click Something", "Make a Selection", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question)

If result = Windows.Forms.DialogResult.Abort Then
    MessageBox.Show("Hey, you Aborted me!")
ElseIf result = Windows.Forms.DialogResult.Retry Then
    MessageBox.Show("You are trying to retry, aren't you?")
ElseIf result = Windows.Forms.DialogResult.Ignore Then
    MessageBox.Show("Don't you dare ignore me!!")
End If

或者

Select Case MessageBox.Show("Click Something", "Make a Selection", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question)
    Case Windows.Forms.DialogResult.Abort
        MessageBox.Show("Hey, you Aborted me!")
    Case Windows.Forms.DialogResult.Retry
        MessageBox.Show("You are trying to retry, aren't you?")
    Case Windows.Forms.DialogResult.Ignore
        MessageBox.Show("Don't you dare ignore me!!")
End Select
于 2013-10-01T12:50:33.867 回答
0

了解每次调用MessageBox.Show方法时都会显示一个新的并返回一个 的。MessageBox DialogResult

为了进一步简化逻辑,考虑下面的伪代码:

If MessageBox.Show Result = Abort
    Show You Aborted Me
Else If MessageBox.Show Result = Retry
    Show You Clicked Retry
Else If MessageBox.Show.Result = Ignore
    Show You Ignored Me

如果用户在第一次出现时单击“Abort”以外的任何内容MessageBox,则运行时将开始执行第二If条语句,该语句将向用户呈现相同但 MessageBox的语句。

重复此过程,直到用户满足If语句的条件或没有更多If语句要考虑。

为了解决问题,您应该确保MessageBox只向用户呈现一次。将 存储DialogResult在局部变量中,然后评估DialogResult

Dim result as DialogResult = MessageBox.Show("Click Something", "Make a Selection", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question) 

If result = DialogResult.Abort Then
    MessageBox.Show("Hey, you Aborted me!")
ElseIf result = DialogResult.Retry Then
    MessageBox.Show("You are trying to retry, aren't you?")
ElseIf result = DialogResult.Ignore Then
    MessageBox.Show("Don't you dare ignore me!!")
Else 
    ' What are you going to do if the user never clicks an applicable button? 
End If
于 2013-10-01T13:00:44.493 回答