|
-
Feb 14th, 2006, 08:10 AM
#1
Thread Starter
Hyperactive Member
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?
Last edited by GrimmReaper; Feb 14th, 2006 at 08:51 AM.
-
Feb 14th, 2006, 08:21 AM
#2
Re: Radiobutton Cast
Well the compiler is correct, Checked is not a member of the Control class, so can't use it. But instead what you can do is this
VB Code:
Dim rbTemp As RadioButton
For Each MPrd1 As Control In Me.Controls
If TypeOf MPrd1 Is RadioButton Then
rbTemp = MPrd1
rbTemp.Checked = False
End If
Next
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 14th, 2006, 08:26 AM
#3
Fanatic Member
Re: Radiobutton Cast
Or just replace this line...
MPrd1.Checked = False
with...
CType(MPrd1,radiobutton).Checked = False
Bob
"I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings
-
Feb 14th, 2006, 08:32 AM
#4
Re: Radiobutton Cast
How could I forget about CType. Bob's way of doing it is better.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 14th, 2006, 08:34 AM
#5
Thread Starter
Hyperactive Member
Re: Radiobutton Cast
Ok, Thanx for both replies,
I have tried both methods, but the radio buttons do not get unchecked
I've got 5 rd buttons in 1 panel, then I have 3 rd buttons in another panel - do you think that it's the panels causing the problems?
-
Feb 14th, 2006, 08:44 AM
#6
Re: Radiobutton Cast
 Originally Posted by GrimmReaper
Ok, Thanx for both replies,
I have tried both methods, but the radio buttons do not get unchecked
I've got 5 rd buttons in 1 panel, then I have 3 rd buttons in another panel - do you think that it's the panels causing the problems?
Well when you have RadioButtons placed on the Panels then you will have to loop through the Controls collection of panel.
VB Code:
For Each ctTemp As Control In Me.Controls
If TypeOf ctTemp Is Panel Then
For Each rbTemp As Control In CType(ctTemp, Panel).Controls
If TypeOf rbTemp Is RadioButton Then
CType(rbTemp, RadioButton).Checked = False
End If
Next
End If
Next
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 14th, 2006, 08:46 AM
#7
Fanatic Member
Re: Radiobutton Cast
You also need to iterate through the controls in each panel. Something along the lines of....
VB Code:
For Each ctl As Control In Me.Controls
'Checks for radiobtns not in panels
If TypeOf ctl Is RadioButton Then
CType(ctl, RadioButton).Checked = False
End If
'checks radion buttons in panels
If TypeOf ctl Is Panel Then
For Each pctl As Control In ctl.Controls
If TypeOf pctl Is RadioButton Then
CType(pctl, RadioButton).Checked = False
End If
Next
End If
Next ctl
Bob
ARGH - Beat me to it.
"I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings
-
Feb 14th, 2006, 08:50 AM
#8
Re: Radiobutton Cast
Here's how to visit every control on a form regradless of how deep it's nested, coupled with unchecking RadioButtons:
VB Code:
Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
Do Until ctl Is Nothing
'Use ctl here.
If TypeOf ctl Is RadioButton Then
DirectCast(ctl, RadioButton).Checked = False
End If
ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
Loop
-
Feb 14th, 2006, 08:50 AM
#9
Thread Starter
Hyperactive Member
Re: Radiobutton Cast
Wow!
I thought that the panel might be the problem - it makes sense to loop through then check if panel etc. because the panel is also a container
Thanx, my friends - you saved my day!
-
Jun 20th, 2017, 10:45 AM
#10
Addicted Member
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.
-
Jun 20th, 2017, 12:01 PM
#11
Addicted Member
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
-
Jun 20th, 2017, 12:03 PM
#12
Addicted Member
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.
-
Jun 20th, 2017, 12:06 PM
#13
Addicted Member
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
-
Jun 20th, 2017, 07:01 PM
#14
Re: Radiobutton Cast [RESOLVED]
 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.
-
Jun 20th, 2017, 07:08 PM
#15
Re: Radiobutton Cast [RESOLVED]
 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.
 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.
-
Jun 21st, 2017, 05:14 PM
#16
Addicted Member
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.
-
Jun 22nd, 2017, 08:00 AM
#17
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.
-
Jun 22nd, 2017, 08:40 AM
#18
Addicted Member
Re: Radiobutton Cast [RESOLVED]
-
Jun 22nd, 2017, 08:50 AM
#19
Addicted Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|