Introduction
Have you ever tried to get a reference to a winform control that you have added to a container? I was bl...y annoyed to find out that Microsoft hadn't included that obvious functionality in VB.NET, so I wrote a function to do it. It requires the control container and the name of the control you are looking for.
Code
Function getControlFromName(ByRef containerObj As Object, _
ByVal name As String) As Control
Try
Dim tempCtrl As Control
For Each tempCtrl In containerObj.Controls
If tempCtrl.Name.ToUpper.Trim = name.ToUpper.Trim Then
Return tempCtrl
End If
Next tempCtrl
Catch ex As Exception
End Try
End Function
Use
To use it, enclose it in a CType function to give you a useful reference to the control. Like this..
Dim tempCtrl As Button = _
CType(getControlFromName(TabControl1.TabPages(tabIndex), "btnName"), Button)
That's it! Hope you find it useful. :D
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here