call with name of object

NicoVB

Centurion
Joined
Jan 2, 2002
Messages
160
Location
Belgium
How do you call a control with it's name defined with a number..


For example

I have a control link3 but I don't know 3, but only have the value hold in a variable 'row'

so.. i tried this:

CType("link" & row, LinkControl).Text = ...


But he gives error, can't convert that string to a linkcontrol!!!

How to do this??

Nico
 
You cant do it that way, the next best thing would be to go through all controls with the for each loop and check if the control has a name of link3.
 
In ASP.NET, the System.Web.UI.Control class has a FindControl method w/c you can use like this:

CType(Page.FindControl("link" & row), LinkControl).Text = ...
 
Hmmm, it seems that a form in Windows app has not such a method. ... What to do now??

mutant >>> I think i have allready so much code in the project that I think it is not a very good way to do this. Maybe it will take a bit too much time.
 
Yeah, I wonder why no FindControl in Windows controls. Closest you get is Controls.Item but it accepts an integer index and not a control name. Creating your own FindControl shouldn't be difficult though.

Public Function FindControl(ByVal parentControl As Control, ByVal vstrCtrlName As String) As Control
Dim ctrl As Control
For Each ctrl In parentControl.Controls
If ctrl.Name = vstrCtrlName Then
Return ctrl
End If
Next
Return Nothing
End Function
 
Back
Top