Using a String value as a RadioButton object reference

M

Mike Bazoo

I am a novice vb.net programmer and hope someone can help me.

For a questionnaire, I have several (100+) radio buttons whose names
follow this standard:
radAlways1, radAlways2, 3.. 10
radUsually1, radUsually2, 3...10
radSometimes1, radSometimes2, 3...10
etc

My ultimate goal is to loop through each one and do an action on the
ones that are 'Checked'.

In order to try to reduce the amount of code, I created Strings inside
each loop which generate radio buttons names. Something like this:

For intCount = 0 To 10
strAlways = "radAlways" & intCount + 1
strUsually = "radUsually" & intCount + 1
strSometimes = "radSometimes" & intCount + 1
strSeldom = "radSeldom" & intCount + 1
strNever = "radNever" & intCount + 1

...'Rest of code here
Next intCount

This part actually works fine which the strings having the right radio
buttons names in each loop iteration.

Now, my question is: how can I use this correctly generated string as
the RadioButton object in order to be able to call RadioButton's
methods ('Checked' in particular)?

For intCount = 0 To 2
strAlways = "radAlways" & intCount + 1
strUsually = "radUsually" & intCount + 1
strSometimes = "radSometimes" & intCount + 1

'HERE IS WHERE I WANT TO USE THE STRING VALUE
'AS THE RADIOBUTTON IN ORDER TO CALL THE 'CHECKED' METHOD.
'I DO NOT WANT TO HARDCODE THE ACTUAL RADIOBUTTON OBJECT
NAME.
If radAlways1.Checked = True Then
'do something
ElseIf radUsually1.Checked = True Then
'do something
ElseIf radSometimes1.Checked = True Then
'do something
End If
Next intCount

I am also open for other ideas on how to accomplish this.
Thanks.
 
C

Cor

Hi Mike,

We did seen this question not for a long while, it was often in this
newsgroup

I type it here new watch typos.

I take the most simple one which asumes that the buttons are all direct on
the form, just to show you how.

When the buttons are on a panel or whatever, there are a lot alternatives
(recursion or whatever).

This is a winform sample in a webform it needs a little bit in an other way.
I did not write it direct in your sample, because I asume that will become
very long.

\\\
dim ctr as control
for each ctr in me.controls
if typeof ctr is radiobutton then
if directcast(ctr,radiobutton).name.substring(0) = "Mybuttonname"
'do what you want to do with ctr.xxxx
end if
end if
next
///
I hope this helps?

Cor
 
R

Russell Jones

Here's a simpler way. Rather than querying the buttons, add a handler to add
the checked radio buttons to a collection--I've used an ArrayList in the
example below, but you might want to use a Dictionary instead if you need to
access the buttons by name. For example:

' somewhere in the Form class
Dim ar As New ArrayList

' in Form Load or when you create the radio buttons, for each button,
' assume you have a reference to the button in the variable rb
AddHandler rb.CheckedChanged, AddressOf RadioButtonCheckChanged

' this event handler adds checked buttons to the collection and removes
unchecked ones.
Private Sub RadioButtonCheckChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs)
If TypeOf sender Is RadioButton Then
Dim rb As RadioButton = CType(sender, RadioButton)
If rb.Checked Then
ar.Add(rb)
Else
If ar.Contains(rb) Then
ar.Remove(rb)
End If
End If
End If
End Sub

Now, whenever a user changes a radio button, that button either gets added
to (checked) or removed from (unchecked) the ArrayList. When the user is
done, the ArrayList contains a list of all the checked buttons. If you think
you still need to use the string name to access a particular button, use a
Dictionary instead, using the control name as the key.
 
B

Bill McCarthy

Alternatively, if it is a ASP.ENT application, then he can use the Page's
FindControl method to return the actual radiobutton given it's name.
The efficiency of which method to use would depend on the number of other
controls on the page. If there are many non radio buttons, or that only some of
the radio buttons information is required in the given code block, or they need
to be retrieved in a given order, then using FindControl would be preferable. If
there are mainly only radiobuttons and all of them need to be enumerate through
in no given order, then using the control's collection would be preferable.

Bill.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Mike Bazoo) scripsit:
For a questionnaire, I have several (100+) radio buttons whose names
follow this standard:
radAlways1, radAlways2, 3.. 10
radUsually1, radUsually2, 3...10
radSometimes1, radSometimes2, 3...10
etc

\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key:

\\\
Private m_Controls As New Hashtable()
///

Adding a control:

\\\
m_Controls.Add(DynamicPictureBox.Name, DynamicPictureBox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Controls.Item("PictureBox1"), PictureBox)
///

Removing a control:

\\\
m_ht.Remove("PictureBox1")
///

Sometimes it's even better to add the control to an array. This will allow
fast and easy index-based access to the control references.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET:
<URL:http://msdn.microsoft.com/library/e...ngControlArraysInVisualBasicNETVisualCNET.asp>
 
C

Cor

Hi Bill,

It is much more simple.

im frm As Control = Me.FindControl("Form1")
Dim ctl As Control
For Each ctl In frm.Controls

I do not know if you knew this one?

Cor
 
B

Bill McCarthy

Hi Herfried,

I would not recommend this for an ASP.ENT application as the Page class already
has a FindControl method.

Bill.
 
C

Cor

Hi Bill,

A half year ago the only message Herfried was sending when there was in the
subject

"ASPNET" or "WEB" or whatever what you could slightly be seen that it was
web was.

"Turn to the newsgroup microsoft.public.dotnet.framework.aspnet"

(You could see he did not even look at the message)

Now I see he is giving answers on Web questions here also.

So please do not bring down all my work in this by pointing him on this?

(And by default we assume in this newsgroup when there not is said Webpage
or page that it is Winform, when I am in doubt I mostly ask that first or
tell that).

:)

Cor
 
B

Bill McCarthy

Hi cor,

Yes, the FindControl method is what I suggested.

BTW: usually the Form and the page have the same Controls on them, so there
would be no point in finding the form first.

Thanks,

Bill.
 
H

Herfried K. Wagner [MVP]

* "Bill McCarthy said:
I would not recommend this for an ASP.ENT application as the Page class already
has a FindControl method.

I know ;-). I'll add that to the FAQ.
 
H

Herfried K. Wagner [MVP]

* "Cor said:
A half year ago the only message Herfried was sending when there was in the
subject

"ASPNET" or "WEB" or whatever what you could slightly be seen that it was
web was.

"Turn to the newsgroup microsoft.public.dotnet.framework.aspnet"

(You could see he did not even look at the message)

Now I see he is giving answers on Web questions here also.

Why do you know that the question is related to web forms?
(And by default we assume in this newsgroup when there not is said Webpage
or page that it is Winform, when I am in doubt I mostly ask that first or
tell that).

IMO Windows Forms is the "default".

:)
 
C

Cor

Herfried,
Why do you know that the question is related to web forms?


IMO Windows Forms is the "default".

We agree completly (as mostly) but I am waiting for that nice Xor (was not
by myself is in a sample on MSDN I thougth about the datagrid)

:))

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
We agree completly (as mostly) but I am waiting for that nice Xor (was not
by myself is in a sample on MSDN I thougth about the datagrid)

Did I miss a post?
 
M

Mike Bazoo

Cor (or anyone),

Your explanation seems to be what I am looking for. However, my loop
is not checking for any of my RadioButtons. I ran it in break mode and
it only checks for other controls such as labels, buttons, etc. It is
very likely that it is because the RadioButtons are inside group boxes
(the GroupBoxes are checked).

See my code below. The question now is: how can I have the
RadioButtons inside the GroupBoxes be checked in the 'For Each ctr In
Me.Controls' statement?

For intCount = 0 To 9

strAlways = "radAlways" & intCount + 1
strUsually = "radUsually" & intCount + 1
strSometimes = "radSometimes" & intCount + 1
strSeldom = "radSeldom" & intCount + 1
strNever = "radNever" & intCount + 1

For Each ctr In Me.Controls
If TypeOf ctr Is RadioButton Then
If DirectCast(ctr, RadioButton).Name = "strAlways" Then
If DirectCast(ctr, RadioButton).Checked = True Then
'Do action
End If
ElseIf DirectCast(ctr, RadioButton).Name = "strUsually"
Then
If DirectCast(ctr, RadioButton).Checked = True Then
'Do action
End If
ElseIf DirectCast(ctr, RadioButton).Name =
"strSometimes" Then
If DirectCast(ctr, RadioButton).Checked = True Then
'Do action
End If
ElseIf DirectCast(ctr, RadioButton).Name = "strSeldom"
Then
If DirectCast(ctr, RadioButton).Checked = True Then
'Do action
End If
ElseIf DirectCast(ctr, RadioButton).Name = "strNever"
Then
If DirectCast(ctr, RadioButton).Checked = True Then
'Do action
End If
End If
End If
Next
Next

Thanks,
Mike
 
C

Cor

Hi Mike,

I just made just an hour ago a very nice routine for that.

If you have problems with it tell me please because I only tested it 15
minutes, so that sure that it works am I not?

Cor

\\\
Dim last As String
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
if typeof ctr is radiobutton
if directcast(ctr,radiobutton).name = "whatever" then
' do your things
end if
end if
doSet(ctr)
Next
End Sub
///
 
Top