You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio中按钮点击事件实现及Retry分支逻辑修正问询

VB.NET MessageBox分支逻辑修正

你的代码里ElseIf分支的写法存在问题,导致无法正确响应Retry按钮的点击:

  • 原代码中ElseIf DialogResult.Retry这一行是错误的:DialogResult.Retry是枚举成员,VB.NET会把它隐式转换为布尔值True(因为它的枚举值不为0)。这意味着只要用户没点Abort,不管点的是Retry还是Ignore,都会执行"retry done"的弹窗,完全不符合预期。

修正后的代码

把MessageBox的返回值先存到变量里,再分别判断各个分支:

Public Class Form1

Private Sub btnhello_Click(sender As Object, e As EventArgs) Handles btnhello.Click
    ' 把弹窗结果存到变量里,避免重复调用弹窗,逻辑更清晰
    Dim userChoice As DialogResult = MessageBox.Show("click anything", "error box", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question)
    
    If userChoice = DialogResult.Abort Then
        MessageBox.Show("aborted")
    ElseIf userChoice = DialogResult.Retry Then
        MessageBox.Show("retry done")
    End If
End Sub
End Class

说明

通过将弹窗的返回值存储到userChoice变量中,后续的If/ElseIf分支可以准确判断用户点击的是哪个按钮,这样点击Retry时会显示"retry done",点击Ignore则不会触发任何分支的弹窗(如果需要处理Ignore的情况,可以再加一个ElseIf或者Else分支)。

内容的提问来源于stack exchange,提问作者Chalana Anantha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 07:27:09