Radiobutton Cast [RESOLVED]
Hello!
I'm trying to loop through all the radiobuttons of a form, and clear their checks
What I'vo got so far is:
VB Code:
Dim MPrd1 As Control
For Each MPrd1 In Me.Controls
If TypeOf MPrd1 Is RadioButton Then
[b]MPrd1.Checked = False[/b]
End If
Next
But now the compiler says that .Checked (the bolded line) is not a member of System.Windows.Forms.Control.
What am I doing wrong?
Re: Radiobutton Cast [RESOLVED]
What if I have two radio buttons in a panel and already check one of them with an specific value or name in run time. I need to know the one checked and in a MsgBox or in a textbox the value or name assing to it.
Re: Radiobutton Cast [RESOLVED]
Dim rbttnM As New RadioButton()
Dim rbttnF As New RadioButton()
For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
For Each subCtrl As Control In ctrl.Controls
If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
MsgBox(subCtrl.Text)
End If
If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
MsgBox(subCtrl.Text)
End If
If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then
If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then
MsgBox("Male")
End If
If CType(subCtrl.Controls("rbttnF"), RadioButton).Checked Then
MsgBox("Female")
End If
End If
Next
End If
Next
End Sub
Re: Radiobutton Cast [RESOLVED]
In code line If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then error :
An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object.
Re: Radiobutton Cast [RESOLVED]
Code:
Dim rbttnM As New RadioButton()
Dim rbttnF As New RadioButton()
For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
For Each subCtrl As Control In ctrl.Controls
If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
MsgBox(subCtrl.Text)
End If
If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
MsgBox(subCtrl.Text)
End If
If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then
If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then
MsgBox("Male")
End If
If CType(subCtrl.Controls("rbttnF"), RadioButton).Checked Then
MsgBox("Female")
End If
End If
Next
End If
Next
End Sub
Re: Radiobutton Cast [RESOLVED]
Quote:
Originally Posted by
ebellounisoft
In code line If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then error :
An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object.
That means that that container has no control with that name in it.
Re: Radiobutton Cast [RESOLVED]
Quote:
Originally Posted by
ebellounisoft
Code:
Dim rbttnM As New RadioButton()
Dim rbttnF As New RadioButton()
For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
For Each subCtrl As Control In ctrl.Controls
If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
MsgBox(subCtrl.Text)
End If
If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
MsgBox(subCtrl.Text)
End If
If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then
If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then
MsgBox("Male")
End If
If CType(subCtrl.Controls("rbttnF"), RadioButton).Checked Then
MsgBox("Female")
End If
End If
Next
End If
Next
End Sub
That code looks rather nonsensical. For one thing, it starts out by creating two RadioButton objects that will never be used.
Quote:
Originally Posted by
ebellounisoft
What if I have two radio buttons in a panel and already check one of them with an specific value or name in run time. I need to know the one checked and in a MsgBox or in a textbox the value or name assing to it.
That question has a very simple answer. You certainly don't need code nearly as complex as that above. If you only have two RadioButtons then just use a simple If...ElseIf statement. The container is then irrelevant.
vb.net Code:
If firstRadioButton.Checked Then
'...
ElseIf secondRadioButton.Checked Then
'...
End If
If you want to handle an arbitrary number of RadioButtons then you can do this:
vb.net Code:
Dim checkedRadioButton = myContainer.Controls.OfType(Of RadioButton)().SingleOrDefault(Function(rb) rb.Checked)
If checkedRadioButton IsNot Nothing Then
'...
End If
You might use the Text property of the RadioButton or, if you want a different value, you can set the Tag in the designer and then use that. If you know for a fact that a RadioButton will be checked then use Single instead of SingleOrDefault and omit the test for Nothing.
Re: Radiobutton Cast [RESOLVED]
CAN SOME ONE HELP ME. HOW DO I START A THREAD. I ASK FOR HELP IN CONTACT US OF VBForums BUT NO ANSWER. WHAT LINK IN MAIN PAGE HAVE TO CLICK TO START NEW THREAD. THANK YOU.
Re: Radiobutton Cast [RESOLVED]
To start a new thread, go to the forum you want to post it in (probably Visual Basic .Net), then click on the "Post New Thread" button.
Re: Radiobutton Cast [RESOLVED]
Re: Radiobutton Cast [RESOLVED]
I can see if it is checked. That is easy,assining a variable to the checked button but what is the code to get the text of the button checked ?. I Try to use MsgBox (ctrl.Text) for the Male button and MsgBox (ctrl.Text) for the Female button, for example, but get an error that can convert a promt to a string.