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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding a button created during runtime 3

Status
Not open for further replies.

BasicBoy

Programmer
Feb 22, 2008
156
ZA
I have made about 100 buttons during runtime, each with a different name and all with tags - some with similar tags, and I have made them all invisible.
On the click event of another button (controlling) I wish to make some of these 100 buttons visible depending on their tag.
When I click the controlling button, I catch its click event, and now I have to find all these other buttons with a certain tag - say 1 - to make them visible.
How to I iterate through all these buttons to find the buttons with this tag.

Like in
On the button click of button btnControlling
Search all these buttons with different names for those which have a tag of 1
Make them visible

Thanks
 
Code:
Private Sub SetControls()
        Dim cControl As Control
        For Each cControl InMe.Controls
            If (TypeOf cControl Is button) Then
                More code here
            End If
        Next cControl
    End Sub
 
MahP's code will work fins as long as you have your buttons placed on the Form itself.

If you place your buttons in any containers, like a GroupBox, you may consider:

Code:
For Each cControl As Control In Me.[blue]GroupBox1[/blue].Controls
    If TypeOf cControl Is Button Then
        More code here
    End If
Next

Have fun.

---- Andy
 

And finally, a recursive function to search through a form and any containers in that form:

Private Function FindControlByName(ByVal ControlName As String, ByVal Container As Control)

Dim cRetVal As Control = Nothing

For Each c As Control In Container.Controls
If c.HasChildren Then
cRetVal = FindControlByName(ControlName, c)
If cRetVal IsNot Nothing Then Exit For
Else
If c.Name = ControlName Then
cRetVal = c
Exit For
EndIf
EndIf
Next

Return cRetVal

End Function

Just pass the form as the control in the first call to the function.

This will work for all containers, no mater how deeply nested in other containers.


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!
 
The smart way to do this is when you create the controls assign them to a collection/s or some other data structure.
 
jebenson, what do you pass as a second parameter to your Function?
"Just pass the [blue]form[/blue] as the control", so I did this:

(if I want to look for a text box named txtLastName)
[tt]
Debug.Print FindControlByName)"txtLastName", [blue]Me[/blue])
[/tt]
But that's not right. Form will not be accespted 'As Control'

Have fun.

---- Andy
 

I just tested this function in both Visual Studio 2008 and 2012. I passed the form as Me, and it worked fine.
I think the problem is you are trying to do "Debug.Print", which expects a String parameter. The function is returning a Control (in your case a Button), not a String.

And I just noticed I did not put a return type on the Function. The definition should read:

Private Function FindControlByName(ByVal ControlName As String, ByVal Container As Control) [red]As Control[/red]


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!
 
Can you please elaborate how I add it to a collection,

Thanks
 

Dim colButtons As Collection

Dim btn As Button

colButtons = New Collection

btn = New Button

btn.Name = "SomeButton"
Me.Controls.Add(btn)

colButtons.Add(btn)

Note that when you add a control to a container (i.e., form, panel, groupbox, etc.), you are adding the control to the container's Controls collection. Using your own collection is just a way to centralize access to the controls, rather than having to search through each container. Just be sure to remove the control from your collection if you ever remove the control from the container.

I prefer to use the recursive function I posted above rather than a collection, because then you don't have to worry about maintaining a collection of controls. Just use the function to find the control you need.




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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top