vb.net 出现对话框后如何停止我的程序执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21323769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:33:20  来源:igfitidea点击:

How do I stop my program from executing after a dialog box appears

vb.net

提问by Tyler Marshall

I have a problem where my code, a simple order form requires certain information. When the user does not enter a piece of information, the program displays a dialog box, and then after the user hits Okay, it still calculates the information. I am new to VB so here is the code snippet:

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

 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)

putting a STOP after each IF statement, results in the program crashing, and I can only have the dialog box say: Okay.

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

Please help!

请帮忙!

回答by T.S.

Here how you should write your program

在这里你应该如何编写你的程序

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

I would also NOT show "Error" message box if someone didn't select something. Error means your application encountered a serious error. This is why there are validators that could display a small red dot next to invalid control, or make back color of control red, etc

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

回答by Khurram Shaikh

Simple Solution for this. Use below code at any place it will stop execution immediately.

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

Response.End

回答by ??ssa P?ngj?rdenlarp

 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

The msgbox message and action dont seem to match. Its not clear that "No item" will END the program (or break to debug in the designer). Maybe you mean Exit Sub? Further, since they have one choice, OK, it will always end the program

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

At any rate, MessageBoxis a function returning a DialogResultyou can evaluate. you dont have your configured that way

无论如何,MessageBox是一个返回一个DialogResult你可以评估的函数。你没有那样配置