For each in Me.Controls


I got many buttons okay. Lets say 6 with their names:   Button1, Button2, Button3 etc.
I want to manipulate for example Button4.

I would like to manipulate that way:

dim str = "Button", x as Integer = 1
("Button" + x).Name .....

I know, we cant do that. I know its possible by the way of a For each than you check the ctrl.Name , then if its then same, you can play with it. But I tought it was not 100% efficiant.  Is there a way like my ("Button" + x)  to call that button other than a for each, other than calling too ... the thing is, it will be never the same button. It might change that why I was needed that integer (x) or a for each

Who is Participating?
Jeff CertainCommented:
Then use Dictionary(of string, button) and put the buttons in the dictionary using the control name as the key...

Imports system.Collections.Generic
Dim controls as Dictionary(Of string, button)

private function GetButton(buttonName as string) as button
If controls.ContainsKey(buttonName) then
  return controls(buttonName)
  return nothing
End If

End Function
Fernando SotoRetiredCommented:
Hi PhilippeRenaud;

Place the controls in a Hash Table with the key as the controls name and then you can get the control using its name.

Fernando SotoRetiredCommented:
What version of .Net are you using 2003 or 2005?
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

PhilippeRenaudAuthor Commented:
Fernando SotoRetiredCommented:
In 2005 you can do this

     Dim btn() As Control = Me.Controls.Find("Button5", True)

PhilippeRenaudAuthor Commented:
I wanted to split points, But i clicked at the wrong place
PhilippeRenaudAuthor Commented:
Well, the hastable do the same thing i guess, but I found it great to do it with a dictionnary.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.