开发者

visual basic if statement that makes the user check at least one box and radio button or they cannot continue

开发者 https://www.devze.com 2023-04-11 14:45 出处:网络
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 proces

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 Obje开发者_C百科ct, 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


If AllAreUnchecked() = True Then
    DisplayMessageBox "At least one category must be checked!"
    Exit Sub
End If


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号