Checkbox/Dropdown Validation Question

lorena

Centurion
Joined
Oct 23, 2003
Messages
134
Location
Phoenix, Arizona
I have a very long webform to be used for insurance changes. I have a series of radio buttons within each section (Medical, Dental, Life Ins, etc) which users click to indicate which type of change they want to make. With Medical it's no problem, we have one type of Medical and it will be for the employee or employee and family.
With Dental, however, I have the radio buttons, two checkboxes and a dropdown. I need to figure out a way to make sure, if the user selected "Add" for the Dental portion, that they also check at least one checkbox and make a selection from the dropdown.
I am probably overthinking it, but I am having a problem figuring out how to do this. Here is what I have so far (dentalBoolean is modulelevel):
Visual Basic:
Sub Check_Dental_Choices()
    Dim dFlag As Boolean
    'Check to see if any Dental selections have been made
    If noDentalRadiobutton.Checked = False Then
      If (empDentalCheckBox.Checked = True Or depDentalCheckBox.Checked = True) _
        And dentalDropDownList.SelectedValue <> "Coverage Type" Then
        dentalBoolean = True
        

      Else
        dFlag = False
      End If
    Else
      'If this NoDental is the choice, all other choices are 
      'disregarded and dentalBoolean is set to false
      dentalBoolean = False
    End If
  End Sub
Is there a way to use Try/Catch here? I want the form to cease processing if the user choices aren't correct.
sorry to be so wordy.
thanks in advance.
 
Exceptions and Try...Catch

Instead of using the dFlag variable (which you are not really using anyway), you could throw an exception if no checkbox has been selected or no coverage type has been selected. Then the code that calls Check_Dental_Choices uses a Try...Catch block and displays the error to the user in the catch section.

Visual Basic:
Public Sub Check_Dental_Choices()
    dentalBoolean = False

    'Check to see if any Dental selections have been made
    If (Not noDentalRadiobutton.Checked) Then
        If Not (empDentalCheckBox.Checked Or depDentalCheckBox.Checked) Then
            'No checkbox is checked, throw an exception
            Throw New Exception("Please check a box")

        Else If (dentalDropDownList.SelectedValue = "Coverage Type") Then
            'No coverage type selected, throw an exception
            Throw New Exception("Please select a coverage type")

        Else
            'Form ok!
            dentalBoolean = True
        End If
    End If
End Sub

And then the calling code:

Visual Basic:
Try
    '... code here

    Check_Dental_Choices()

    '... more code

Catch ex As Exception

    'Display problem to user
    Me.lblError.Text = "A problem occurred with your selection: " & ex.Message

End Try

Good luck :)
 
Back
Top