Simple loop question

Joined
Mar 13, 2007
Messages
5
Programming Experience
Beginner
I have an application that has 8 check boxes on it. I want to verify that all the boxes are checked. If at least one is not checked then I want a message to pop up tell the user to make a valid selection and then have it go back to the form and allow the use to make a choice and reprocess the checkbox validation. I can verify the checkboxes and pop up a message telling the user to make a valid selection, but I can't figure out how to stop the process to allow the user to make a valid selection and try again. Here is my code for this section:

Sub Apps_Checked()

Dim Valid_Checkboxes As Int16

Do Until Valid_Checkboxes = 1
If Me.chkMicrostation.Checked = False And Me.chkGeopak.Checked = False And Me.chkInterplot.Checked = False _
And Me.chkProjectWise.Checked = False And Me.chkCadWorkspaces.Checked = False And Me.chkUpdateH.Checked = False _
And Me.chkAutoTURN.Checked = False And Me.chkAxiom.Checked = False And Me.chkUltraEdit.Checked = False Then
Valid_Checkboxes = 0
MessageBox.Show("Please enter a valid choice")
Else
Valid_Checkboxes = 1
End If
Loop

End Sub

It's very simple I no so don't laugh to hard when you read it.

Thank you for any help.

-Phillip
 
Try this..

VB.NET:
For Each Ctrl as Control In Me.Controls

If Ctrl.GetType() Is GetType(CheckBox) AndAlso Not DirectCast(Ctrl, Checkbox).Checked Then
Ctrl.ForeColor = Color.Red
End If
Next



MessageBox.Show("Some Title", "Please Ensure All Fields Marked In Red Have A Valid Value")

I just wrote this from memory at the end of a very long day so I hope it works. Whilst I'm sure that there are easier ways to do this. That's my effort. This is also assuming you are doing this from within the containing class behind your form.
 
Change this:
VB.NET:
If Ctrl.GetType() Is GetType(CheckBox)
to this:
VB.NET:
If TypeOf Ctrl Is CheckBox
That said, what's the point of all these CheckBoxes if the user is obliged to have them all checked? If there's no choice then why provide one?
 
Using this as an example then how would you verify that at least one check box or one radio button was checked in a group box in a form before allowing the user to submit this information.

I have a form with three group boxes of radio buttons, (groupBox1, groupBox and groupBox3) and there needs to be a selection from each groupBox. I have a groupBox of checkboxes (1 - 10) where at least one item needs to be checked.

I played around with similar code to what was posted but have not been able to come up with anything that works.

Thanks
Coach Barker
 
There's no need to validate when the user submits. It is vastly preferable to disable the OK/Submit button if the required fields haven't been populated. For a group of RadioButtons you can simply created one CheckedChanged event handler for all of them. When the event is raised the first time you know that an item has been selected and once that's done they user can't deselect without selecting another. That means that you can use a Boolean variable that is initially False and set to True in the event handler if the 'sender' is checked.

As for CheckBoxes, you could use an Integer variable that is initially zero. Again you'd use one event handler for all. If the 'sender' is checked you increment the variable and decrement if its unchecked. You'd call a method from each of these event handlers and enable the OK/Submit button if and only if a RadioButton is checked and at least one CheckBox is checked:
VB.NET:
Me.okButton.Enabled = Me.isRadioButtonChecked AndAlso Me.checkedBoxCount > 0
 
Could you post an example of how you would do this, I have never had to specifcally deal with event handlers before. Would you create one function for the radio buttons for each group box, or one that would check all three group boxes?

Thanks
Coach Barker
 
One for each group, and I'm afraid I don't believe this:
I have never had to specifcally deal with event handlers before.
Have you ever made something happen when a Button was clicked or when a Form was loaded? If so then you've specifically dealt with event handlers.
 
Let me rephrase my response. Yes I have dealt with Form_load events, Button_Click events, text changed, selected index changed and even
RadioButton_CheckedChanged events, but always in a single instance of the control.

Such as declaring a global variable such as

VB.NET:
Private hasChanged As Boolean = False

and then checking each and every control such as

VB.NET:
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        hasChanged = True
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        hasChanged = True
    End Sub

Until I saw the code in this thread I assumed it was possible to check multiple controls, but as I said I was unable to get it to work. Using a version of the code posted I am able to clear all the radio buttons on a form by doing this

VB.NET:
Sub clearRadioButtons()
        Dim ctrl As Control = Me.GetNextControl(Me, True)
        While ctrl IsNot Nothing
            If TypeOf ctrl Is RadioButton Then
                DirectCast(ctrl, RadioButton).Checked = False
            End If
            ctrl = Me.GetNextControl(ctrl, True)
        End While
    End Sub

Using the same code but replacing RadioButton with CheckBox I can clear all chaeckBoxes. I assume the code to check if a radio button (or Check Box)has been checked would be along these lines, I will continue to work on it.

Thanks for your advice
Coach Barker
 
If you want the same method to handle more than one event then you add more than one event to the Handles clause. You can do this by manually typing it or you can select an event handler for a design-time element in the Properties window by first pressing the Events button. If you need to you can then determine which object raised the event using the 'sender' parameter, which is always a reference to the object that raised the event.
 
Back
Top