Question Unable to cast object of type error !

kapkap

New member
Joined
Mar 28, 2011
Messages
3
Programming Experience
Beginner
i write this code to work with some radio button and calculate Score for a questionare :

Dim SScore As Byte = 0
Dim BTNN As Byte = 0
Dim BTNNS As String = ""
For Each rBTN As RadioButton In Me.Controls
If Mid(rBTN.Name, 1, 5) = "RBS01" Then
If rBTN.Checked Then
SScore = SScore + CType(Mid(rBTN.Name, 7, 1), Byte)
End If
End If
Next

I get this error on line 4 :
Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.Forms.RadioButton'.

i dont know what is it and how can handle it. please help me.
 
At the top of the loop add the typeof function to make sure the control is a radio button, not every control in me.controls is a radio button

VB.NET:
If typeof rbtn is radiobutton then

The syntax may be slightly off but the key lies within the typeof function used so you only perform your functions on radio button controls.
 
It is because you have a button(s) on your form (in addition to the radio buttons)

You need to specify the typeof control to iterate through as radiobuttons only.
 
Last edited:
Thank u for u help but i forgot to say somethings
1. i had a bit of code first of all :
dim rBTN as Radiobutton
2. i have my radiobuttons on a groupboxes that are in Tablelayoutpanel that are in some Tabcontrols that are in another tabcontrol that is on my Form. oh!

because of error that appear in "ForEach rBTN AsRadioButtonInMe.Controls
" i guess problem is diffrent. thanks for your help and i become glade to help me again. :)
 
Have you resolved this issue or are you still in need of assistance? If so, please change the thread status to Answered. If not, you should have made that clear in your last post because that could be taken either way.
 
hi,
sorry , i am new here and i dont know , how to clear "answered" tag. please guide me. i edited post and went to advance but i dident see any option about it. Thanks
 
Back
Top