[英]Visual Basic how to stop an event after MsgBox is shown
I want to make sure that the user has checked one of my 3 checkboxes before my furthur process, thus I created the function below and generate a Msg if they didn't checked anything.我想确保用户在我的进一步处理之前检查了我的 3 个复选框之一,因此我在下面创建了 function 并在他们没有检查任何内容时生成一个 Msg。 the issue is the process still carries on after the msg box.
问题是在消息框之后该过程仍在进行。 So is there any way to stop this event after this particular msgbox is shown?
那么在显示这个特定的 msgbox 之后有什么方法可以阻止这个事件?
Private Sub btnPay_Click(sender As Object, e As EventArgs) Handles btnPay.Click
If chkbNotes.Checked = True Then
conn.Open()
sql = "UPDATE Order SET payment_method_id = 1 WHERE Id = '" + FoodMenu.OrderID + "';"
cmd = New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
Else
If chkbCreditCard.Checked = True Then
conn.Open()
sql = "UPDATE Order SET payment_method_id = 2 WHERE Id = '" + FoodMenu.OrderID + "';"
cmd = New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
Else
If chkbEwallet.Checked = True Then
conn.Open()
sql = "UPDATE Order SET payment_method_id = 2 WHERE Id = '" + FoodMenu.OrderID + "';"
cmd = New SqlCommand(sql, conn)
cmd.ExecuteNonQuery()
Else
MsgBox("Please choose 1 Payment Method", MsgBoxStyle.Exclamation, "Error")
End If
End If
End If
Dim Amount As String
Amount = InputBox("Enter Paying Amount (EX:100.00)", "Amount")
If (IsNumeric(Amount)) = True Then
.
.
.
Here's what your code ought to look like:这是您的代码应如下所示:
Private Sub btnPay_Click(sender As Object, e As EventArgs) Handles btnPay.Click
Dim paymentMethodId As Integer
If chkbNotes.Checked Then
paymentMethodId = 1
ElseIf chkbCreditCard.Checked Then
paymentMethodId = 2
ElseIf chkbEwallet.Checked Then
paymentMethodId = 3
Else
MessageBox.Show("Please choose 1 Payment Method", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
End If
sql = "UPDATE Order SET payment_method_id = @PaymentMethodId WHERE Id = @Id"
cmd = New SqlCommand(sql, conn)
cmd.Parameters.Add("@PaymentMethodId", SqlDbType.Int).Value = paymentMethodId
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = FoodMenu.OrderID
conn.Open()
cmd.ExecuteNonQuery()
I have taken the liberty of assuming that the payemtn method ID should be 3 in the third case rather than 2, which you were using in your original code.我冒昧地假设 payemtn 方法 ID 在第三种情况下应该是 3,而不是您在原始代码中使用的 2。
I would also recommend changing your ADO.NEt code as it appears that you are reusing connection and command objects.我还建议更改您的 ADO.NEt 代码,因为您似乎正在重用连接和命令对象。 You should just create and destroy them where and when you need them.
您应该在需要它们的时间和地点创建和销毁它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.