Visual Basic if 语句,使用户至少选中一个框和单选按钮,否则他们无法继续
这就是我到目前为止针对以下场景的情况:
我希望这样,如果用户单击“提交”而没有选中某个部分中的复选框,则会弹出一个对话框,使流程循环并让他们有机会检查一个盒子。在至少选中一个复选框之前,该过程不会继续。上面同样的事情也适用于我的单选按钮。这是我的代码:
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
'If everything is unchecked then show message box
If checkHardware.Checked And checkNetwork.Checked And checkOther.Checked And checkSoftware.Checked And _
checkPassword.Checked And checkPermissions.Checked = False Then
'Assign the message box boolean to variable
Dim boolMsgBox As Boolean
boolMsgBox = True
While boolMsgBox = True
'NO checks means NO submit and must select OK on message box to continue
Dim msg1 As String
Dim title1 As String
Dim style1 As MsgBoxStyle
Dim response1 As MsgBoxResult
msg1 = "At least one category must be checked!"
style1 = MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly
title1 = "Check a category please"
response1 = MsgBox(msg1, style1, title1)
If response1 = MsgBoxResult.Ok Then
boolMsgBox = False
End If
'Loops back to the top and looks again for check marks after submit is clicked
End While
End If
If rbHigh.Checked And rbMedium.Checked And rbLow.Checked And rbNone.Checked = False Then
Dim boolrb As Boolean
boolrb = True
While boolrb = True
Dim msg2 As String
Dim title2 As String
Dim style2 As MsgBoxStyle
Dim response2 As MsgBoxResult
msg2 = "At least one priority must be Checked!"
style2 = MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly
title2 = "Check a priority level please"
response2 = MsgBox(msg2, style2, title2)
If response2 = MsgBoxResult.Ok Then
boolrb = False
End If
End While
End If
End Sub
This is what I have so far for the following scenario:
I want to have it so that if a users clicks submit without checking a check box from one section it throws up a dialog box that makes the process loop and give them a chance to check a box. The process wont continue until at least one check box is checked. The same thing above goes for my radio buttons as well. Here's my code:
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
'If everything is unchecked then show message box
If checkHardware.Checked And checkNetwork.Checked And checkOther.Checked And checkSoftware.Checked And _
checkPassword.Checked And checkPermissions.Checked = False Then
'Assign the message box boolean to variable
Dim boolMsgBox As Boolean
boolMsgBox = True
While boolMsgBox = True
'NO checks means NO submit and must select OK on message box to continue
Dim msg1 As String
Dim title1 As String
Dim style1 As MsgBoxStyle
Dim response1 As MsgBoxResult
msg1 = "At least one category must be checked!"
style1 = MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly
title1 = "Check a category please"
response1 = MsgBox(msg1, style1, title1)
If response1 = MsgBoxResult.Ok Then
boolMsgBox = False
End If
'Loops back to the top and looks again for check marks after submit is clicked
End While
End If
If rbHigh.Checked And rbMedium.Checked And rbLow.Checked And rbNone.Checked = False Then
Dim boolrb As Boolean
boolrb = True
While boolrb = True
Dim msg2 As String
Dim title2 As String
Dim style2 As MsgBoxStyle
Dim response2 As MsgBoxResult
msg2 = "At least one priority must be Checked!"
style2 = MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly
title2 = "Check a priority level please"
response2 = MsgBox(msg2, style2, title2)
If response2 = MsgBoxResult.Ok Then
boolrb = False
End If
End While
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您只需将行
boolMsgBox = False
替换为Exit Sub
即可。这将使该函数不处理任何内容,并允许用户在再次单击按钮之前更正表单。第二个 while 循环也是如此,只需用 exit sub 替换相应的行即可。I think you just need to replace the line
boolMsgBox = False
withExit Sub
. This will leave the function without processing anything and allow the user to correct the form before clicking the button again. Ditto on the second while loop, just replace the corresponding line with exit sub.