jalo Posted August 23, 2005 Posted August 23, 2005 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. Quote
Administrators PlausiblyDamp Posted August 23, 2005 Administrators Posted August 23, 2005 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jalo Posted August 23, 2005 Author Posted August 23, 2005 (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 August 23, 2005 by jalo Quote
*Experts* DiverDan Posted August 23, 2005 *Experts* Posted August 23, 2005 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 Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Machaira Posted August 23, 2005 Posted August 23, 2005 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. Quote Here's what I'm up to.
*Experts* DiverDan Posted August 23, 2005 *Experts* Posted August 23, 2005 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 Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
jalo Posted August 23, 2005 Author Posted August 23, 2005 DiverDan - Your solution works wonderfully :-)) thanks! (I wrote my second post before you had posted your soluion...) Quote
SonicBoomAu Posted August 23, 2005 Posted August 23, 2005 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. Quote 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 snarfblam Posted August 23, 2005 Leaders Posted August 23, 2005 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. Quote [sIGPIC]e[/sIGPIC]
SonicBoomAu Posted August 23, 2005 Posted August 23, 2005 Thanks for that Marble Eater. It isn't my problem I was just curious. Quote 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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.