将值添加到多个文本框(相似名称) [英] Add values to multiple textboxes (similar names)
问题描述
我试图为此找到解决方案,但没有任何效果.我有大约650个文本框名为:A001A,A002A ... A600A,我想在这些texbox中放置值,例如"example value".
I was trying to find the solution for this but nothing works. I have around 650 textboxes named : A001A, A002A...A600A and I would like to put values in those texboxes for instance "example value".
我找到了将值以某种形式放入所有文本框中的解决方案,但在我的项目中却有一些具有其他值的解决方案.
I've found solution to put values in all textboxes in a form, but in mine there are those with some other values.
下一个解决方案是这个:
Next solution is this one :
Dim textBoxArr() As TextBox = {A2, A3, A4, A5, etc..}
For Each tb As TextBox In textBoxArr
Select Case tb.Text
Case "RESNO"
tb.Text = "-15,55,0"
Case "DOGAL"
tb.Text = "-15,54,0"
End Select
Next
...同样,我必须输入所有名称,并且我没有足够的自由来操纵前50个值,依此类推.
...again I have to input all names and I don't have enough freedom to manipulate values for first 50 and so on.
这是我拥有的代码,它不起作用:
This is the code I have and it doesn't work:
For I As Integer = 0 To 650
Dim txt As TextBox = DirectCast(Me.Controls("A00" & I.ToString & "A"), TextBox)
txt.Text = "example value"
Next
错误:对象引用未设置为对象的实例
感谢您的帮助.
我已经从用户Idle_Mind
EDIT : I have found the solution from user Idle_Mind
Dim matches() As Control
For i As Integer = 1 To 650
matches = Me.Controls.Find("A" & String.Format("{0:000}", i) & "A", True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim cb As TextBox = DirectCast(matches(0), TextBox)
If cb.Text = "" Then
cb.Text = "test"
End If
End If
Next
推荐答案
您没有测试正确的键(如果您的TextBox名称正确).
You aren't testing for the right key (if your TextBoxes are properly named).
尝试这样:
For i As Integer = 1 To 650
Dim key As String = "A" & String.Format("{0:000}", i) & "A"
If Me.Controls.ContainsKey(key) Then
Me.Controls(key).Text = "blah"
End If
Next
这篇关于将值添加到多个文本框(相似名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!