Reputation: 447
I'm working in VB.NET and I'm rather new. I'm doing a search for each button on a panel with:
For Each _x_ As Control In _y_.Controls
I have the y as a string name, is there a way to convert this string to the actual panel or controls group that is associated so I don't have to code for each individually?
Is there an easier way to do this?
i.e.
private function return_button(ByVal y As integer)
Dim z As String = R_ + ToString(y)
(z is now the panel name I am using, but it's a string not the panel itself)
Upvotes: 1
Views: 1434
Reputation: 447
I figured a solution to this issue. For whatever reason it wouldn't allow me to cast the variables to an instantiated value, it always remained null/nothing. So instead I wrote:
Dim p As String = "<String related to the panel I am looking for>"
Dim panel() = Me.Controls.Find(p, true)
Now I can prove the panels are equal by calling:
MsgBox(panel(0).GetHashCode = <panel name>.GetHashCode)
and was now certain they were the same object so I did a the for loop and it worked with the panel. Now I can loop for the entire TabPage and find all the Panels on this tab. That will make my code far more streamlined than it was prior. Thank you for setting me on the right path, Steven.
Upvotes: 0
Reputation: 43743
All controls are referenced in their parent's Controls
collection. So, if the panel you are looking for is a child of your form, you can look for it in your form's Controls
collection. The Controls
collection is indexed by the controls' names, so you can retrieve them easily by name, like this:
Dim z As String = ...
Dim p As Panel = DirectCast(Me.Controls(z), Panel)
Upvotes: 1