Re: Using a String value as a RadioButton object reference
From: Russell Jones (arj1_at_nospam.northstate.net)
Date: 03/30/04
- Next message: Cor: "Re: drop down list in datagrid"
- Previous message: Cor: "Re: Using a String value as a RadioButton object reference"
- In reply to: Mike Bazoo: "Using a String value as a RadioButton object reference"
- Next in thread: Herfried K. Wagner [MVP]: "Re: Using a String value as a RadioButton object reference"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 30 Mar 2004 02:00:23 -0500
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.
"Mike Bazoo" <mike_bazoo@hotmail.com> wrote in message
news:62023689.0403292020.7ec480d0@posting.google.com...
> 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.
- Next message: Cor: "Re: drop down list in datagrid"
- Previous message: Cor: "Re: Using a String value as a RadioButton object reference"
- In reply to: Mike Bazoo: "Using a String value as a RadioButton object reference"
- Next in thread: Herfried K. Wagner [MVP]: "Re: Using a String value as a RadioButton object reference"
- Messages sorted by: [ date ] [ thread ]