Visual Basic if 语句,使用户至少选中一个框和单选按钮,否则他们无法继续

发布于 2024-12-08 01:40:42 字数 2003 浏览 0 评论 0原文

这就是我到目前为止针对以下场景的情况:

我希望这样,如果用户单击“提交”而没有选中某个部分中的复选框,则会弹出一个对话框,使流程循环并让他们有机会检查一个盒子。在至少选中一个复选框之前,该过程不会继续。上面同样的事情也适用于我的单选按钮。这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-12-15 01:40:42
If AllAreUnchecked() = True Then
    DisplayMessageBox "At least one category must be checked!"
    Exit Sub
End If
If AllAreUnchecked() = True Then
    DisplayMessageBox "At least one category must be checked!"
    Exit Sub
End If
岁月苍老的讽刺 2024-12-15 01:40:42

我认为您只需将行 boolMsgBox = False 替换为 Exit Sub 即可。这将使该函数不处理任何内容,并允许用户在再次单击按钮之前更正表单。第二个 while 循环也是如此,只需用 exit sub 替换相应的行即可。

I think you just need to replace the line boolMsgBox = False with Exit 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文