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?

Improve company productivity with a Business Account.Sign Up

Jeff CertainConnect With a Mentor Commented:
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 SotoConnect With a Mentor RetiredCommented:
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?
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.