iterating through checkboxes in a groupbox - invalid cast?

jalo

Freshman
Joined
Aug 9, 2005
Messages
28
i am trying to iterate through the checkboxes in a groupbox to see which ones are selected so that i can insert the info into the datavase. I am getting a cast exception at the "For each ..." line - i wonder if a checkbox is something other than a control???

Code:
Dim chkSelected As CheckBox
[COLOR=Red]For Each chkSelected In GroupBox1.Controls[/COLOR]
     chkSelected = CType(Controls.Item("chkSelected"), CheckBox)
     If chkSelected.Checked = True Then
           'set the Meds field of the command
           updateCommand3.Parameters.Item("Meds").Value() = chkSelected.Text

           'update the database
           updateCommand3.ExecuteNonQuery()
      End If
Next

The exception:
System.InvalidCastException: Specified cast is not valid.
 
Does GroupBox1 contain anything other than CheckBoxes? If so that will cause it to fail. You will need to just cast to a control and then check if it is a checkbox
 
oh.... nevermind - i was referring to the wrong groupbox.... however, when i fixed that, now i get this error:

Code:
System.InvalidCastException: Cast from string "chkSelected" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format.
   at Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value)
   at Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value)
   --- End of inner exception stack trace ---
   at Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value)
   at Main_App.TypeOfStudy.InsertMedications() in C:\Visual Basic stuff\Main App\Form2.vb:line 713

i don't have any integers in that groupbox, nor any of the text values for the checkboxes are integers. any ideas?

line 713 refers to:
chkSelected = CType(Controls.Item("chkSelected"), CheckBox)
 
Last edited:
I think what PlausiblyDamp is refering to is more like:

Visual Basic:
Dim ctrl as Control
Dim ckBox as CheckBox
For Each ctrl in GroupBox1.Controls
    If TypeOf ctrl is CheckBox Then
        ckBox = DirectCast(ctrl, CheckBox)
        If ckBox.Checked Then
            'Blah
        End If
    End If
Next
 
jalo said:
oh.... nevermind - i was referring to the wrong groupbox.... however, when i fixed that, now i get this error:

Code:
System.InvalidCastException: Cast from string "chkSelected" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format.
   at Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value)
   at Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value)
   --- End of inner exception stack trace ---
   at Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value)
   at Main_App.TypeOfStudy.InsertMedications() in C:\Visual Basic stuff\Main App\Form2.vb:line 713

i don't have any integers in that groupbox, nor any of the text values for the checkboxes are integers. any ideas?

line 713 refers to:
chkSelected = CType(Controls.Item("chkSelected"), CheckBox)
You can't use a varaible name as an index into the controls collection, it has to be a number.
 
Why are you still using "chkSelected = CType(Controls.Item("chkSelected"), CheckBox)"?

Just cut and paste my snippet without changing anything and see what happens.
Visual Basic:
Dim ctrl as Control
Dim ckBox as CheckBox
For Each ctrl in GroupBox1.Controls
    If TypeOf ctrl is CheckBox Then
        ckBox = DirectCast(ctrl, CheckBox)
        If ckBox.Checked Then
            MessageBox.Show(ckBox.Name)
        End If
    End If
Next
 
DiverDan - Your solution works wonderfully :-)) thanks!

(I wrote my second post before you had posted your soluion...)
 
DiverDan,

What is the differnce between Ctype and DirectCase?
Is it because you are searching through a groupbox and not the form (me.controls) ?

Sorry if this is a stupid question but I am just trying to figure out why Ctype didn't work.
 
I know that I am not DiverDan, but anyways... CType is a more general purpose casting/converting statement. It will convert strings to integers, integers into strings, either of those into objects... any valid cast or conversion.

DirectCast is a simple type cast, it does no converting. The cast must be valid without conversion, or an error will occur. You can DirectCast a Form to a Control; since Form derives from Control, all forms are controls. If you have a variable of type control that points to a Form, you can DirectCast to a Form.

Your problem is not whether you used CType or DirectCast. You were, somewhere in your code, trying to convert from a string ("chkSelected") to an integer and the conversion is not valid. You could swap CType for DirectCast in the code above and it will still work fine.
 
Back
Top