有没有一种方法可以将诸如0、1和2之类的字符插入文本框名称,因为我有一个名为TB_Result0,TB_Result1和TB_Result2的文本框?
num(counter) = "TB_Result" & counter & ".text"
我可以这样做:
num(0) = TB_Result0.Text
num(1) = TB_Result1.Text
num(2) = TB_Result2.Text
谢谢
答案 0 :(得分:2)
类似的事情可能会起作用
For i = 0 To 2
num (i) = Me.Controls("TB_Result" & i)
Next
答案 1 :(得分:0)
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.