【问题标题】:How do I stop my program from executing after a dialog box appears出现对话框后如何停止程序执行
【发布时间】:2018-10-08 22:44:07
【问题描述】:

我有一个问题,我的代码,一个简单的订单表格需要某些信息。当用户没有输入一条信息时,程序会显示一个对话框,然后用户点击 Okay 后,它仍然会计算信息。我是 VB 新手,所以这里是代码 sn-p:

 If (CheckBox3.Checked = False And blueBlackQuantityTextBox.Text <> "" And blueBlackQuantityTextBox.Text > 0) Then
        MessageBox.Show("Please check item you wish to purchase", "No Item Selected",
      MessageBoxButtons.OK, MessageBoxIcon.Error)
        Stop
    End If

    Const whiteBlackDicePrice = 6.25
    Const redBlackDicePrice = 5.0
    Const blueBlackDicePrice = 7.5
    Const tax = 0.05

    Dim whiteBlackSubTotal As Double = whiteBlackQuantityTextBox.Text *      whiteBlackDicePrice
    Dim redBlackSubTotal As Double = redBlackQuantityTextBox.Text * redBlackDicePrice
    Dim blueBlackSubtotal As Double = blueBlackQuantityTextBox.Text * blueBlackDicePrice
    'newBalanceResultLabel.Text = String.Format("{0:C2}", newBalance)

    Dim subtotal As Double = whiteBlackSubTotal + redBlackSubTotal + blueBlackSubtotal

    whiteBlackTotalsLabel.Text = String.Format("{0:C2}", whiteBlackSubTotal)
    redBlackTotalsLabel.Text = String.Format("{0:C2}", redBlackSubTotal)
    blueBlackTotalsLabel.Text = String.Format("{0:C2}", blueBlackSubtotal)

    subtotalResultLabel.Text = String.Format("{0:C2}", subtotal)

在每个 IF 语句后加上一个 STOP,会导致程序崩溃,我只能让对话框说:好的。

请帮忙!

【问题讨论】:

  • 我有一个临时解决方案。 dim count as integer = 0,并在每个 IF 语句后将 count 加 1。如果 count 为 = 0,则继续执行程序。谁能提供更好的解决方案?
  • 你只显示一个 IF 语句

标签: vb.net


【解决方案1】:

这里应该如何编写程序

Private sub MySub()

    If not ConditionsValidated() Then

        MessageBox.Show("Please check item you wish to purchase") ' MsgBox with a single button always returns Ok. Here you don't even need to tell to show Ok - this is default.
        Return
    End If

    ' your code is running here

end sub

private function ConditionsValidated() as Boolean

    ' Validate your controls here

end sub

如果有人没有选择某些内容,我也不会显示“错误”消息框。错误意味着您的应用程序遇到了严重错误。这就是为什么有些验证器可以在无效控件旁边显示一个小红点,或者将控件的背景颜色设为红色等

【讨论】:

    【解决方案2】:
     If (CheckBox3.Checked = False And blueBlackQuantityTextBox.Text <> "" 
           And blueBlackQuantityTextBox.Text > 0) Then
    
        If MessageBox.Show("Please check item you wish to purchase", 
               "No Item Selected",
               MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
              Stop
        End If
     End if
    

    msgbox 消息和操作似乎不匹配。目前尚不清楚“无项目”是否会结束程序(或在设计器中中断调试)。也许你的意思是Exit Sub?此外,既然他们只有一个选择,OK,它总是会结束程序

    无论如何,MessageBox 是一个返回 DialogResult 的函数,您可以对其进行评估。你没有这样配置

    【讨论】:

    • 对不起,发生了什么事: 1. 运行程序。 2. 用户出错。 3. 对话框显示“请检查...购买” 4. 信息仍在计算(if 检查后的代码。) 我想在错误消息显示后,用户可以更改信息,而无需计算代码。
    • 最初的问题是在 MsgBox 之后使程序停止,上面解释了 - 你没有检查结果。现在听起来你想要的是 TS answer - 检查数据并与用户进行对话,从计算中分离出来。或者代替 STOP,执行 EXIT SUB 或更改 IF 结构以使用 ELSE
    【解决方案3】:

    对此的简单解决方案。在任何地方使用下面的代码,它将立即停止执行。

    Response.End
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多