Lochlan Sharkey
Lochlan Sharkey

Reputation: 1

how to insert charecter into object name?

is there a way to insert a character such as 0, 1 and 2 into a text box name, as i have a text box named TB_Result0, TB_Result1 and TB_Result2?

num(counter) = "TB_Result" & counter & ".text"

I can do it instead by doing:

    num(0) = TB_Result0.Text
    num(1) = TB_Result1.Text
    num(2) = TB_Result2.Text

Thanks

Upvotes: 0

Views: 32

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39122

Assuming VB.Net, you can search for the control, which will find it no matter how far nested it is inside containers other than the form itself:

For i As Integer = 0 To 2
    Dim ctl As Control = Me.Controls.Find("TB_Result" & i, True).FirstOrDefault
    If Not IsNothing(ctl) Then
        num(i) = ctl.Text
    End If
Next

The Find function will search recursively into nested containers looking for matches. It returns an array of matches as it is possible to have more than one control with the same name (usually due to dynamic controls created at run time). The FirstOrDefault part gives you either the first element in the returned array, or the default value, which in this case will be Nothing. Lastly, if "ctl" isn't Nothing then we have a match and do something with it.

Upvotes: 0

urdearboy
urdearboy

Reputation: 14580

Something like this may work

    For i = 0 To 2
        num (i) = Me.Controls("TB_Result" & i)
    Next

Upvotes: 2

Related Questions