Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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???

 

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.

Posted (edited)

oh.... nevermind - i was referring to the wrong groupbox.... however, when i fixed that, now i get this error:

 

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)

Edited by jalo
  • *Experts*
Posted

I think what PlausiblyDamp is refering to is more like:

 

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

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted
oh.... nevermind - i was referring to the wrong groupbox.... however, when i fixed that, now i get this error:

 

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.

Here's what I'm up to.
  • *Experts*
Posted

Why are you still using "chkSelected = CType(Controls.Item("chkSelected"), CheckBox)"?

 

Just cut and paste my snippet without changing anything and see what happens.

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

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

DiverDan - Your solution works wonderfully :-)) thanks!

 

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

Posted

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.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]
Posted
Thanks for that Marble Eater. It isn't my problem I was just curious.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...