Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a string for an object name

Status
Not open for further replies.

greymonkey

Technical User
Jul 20, 2003
29
0
0
AU
Hi There,

I have a group of RadioButtons and a for loop which run through them checking if they are selected.

for i = 1 to numOfButtons
tempName = "RadioButton" & i
if tempname.checked then
do somthing
end if

next


The trouble I am having is that the tempName.checked is not being reconised and is returning errors.

Does anyone know how this could be overcome.

Thanks,


 
This post: thread796-1009335

shows the following method for getting a control by its name:


Public Function GetControlByName(ByVal Name As String) As Control

'now, why would I put a "_" in front of the name?
Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, _
System.Reflection.BindingFlags.NonPublic Or _
System.Reflection.BindingFlags.Instance Or _
System.Reflection.BindingFlags.Public Or _
System.Reflection.BindingFlags.IgnoreCase)

If info Is Nothing Then Return Nothing
Dim o As Object = info.GetValue(Me)
Return o

End Function

For your purposes, you would use it like this:

for i = 1 to numOfButtons
tempName = "RadioButton" & i
if [red]CType(GetControlByName(tempName), RadioButton)[/red].Checked then
do something
end if
next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top