对按钮进行多次单击

时间:2013-10-01 12:40:54

标签: vb.net

我正在通过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

3 个答案:

答案 0 :(得分: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 ...
   ...

答案 1 :(得分: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

OR

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

答案 2 :(得分:0)

了解每次调用MessageBox.Show方法时,都会显示 MessageBox,并且 new 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

如果用户在首次出现MessageBox时单击“Abort”以外的任何内容,则运行时将开始执行第二个If语句,该语句将显示相同但 new 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