我有一个VB6用户控件,文本框名为txtIndexData(0),txtIndexData(1),txtIndexData(2)等。 用户控件的代码使用for循环和递增计数器来处理每个文本框(或特定数字文本框),代码如下:
' Check each of the text boxes to see if
' any are currently editing a text constant. If
' so, go back to the previous scroll setting
' and allow the text box to finish before scrolling
For I = 0 To LINK_BOX_SIZE - 1
If txtIndexData(I).Locked = False Then
CurrentScrollValue = vsbIndex.Value
vsbIndex.Value = LastScrollValue
Call txtIndexData_LostFocus(I)
vsbIndex.Value = CurrentScrollValue
End If
Next I
当试图在VB.Net中复制它时,txtIndexData(1)显然是文本框的无效标识符(因为我猜的括号)。我应该如何命名这些,以便我可以根据插入名称的变量(如上所述)编写影响某些文本框的函数?
很抱歉我缺乏VB6 / VB.NET / WinForms知识。我知道我可能会遗漏一些东西。谢谢你的时间。
答案 0 :(得分:1)
我在.Net中写了这样的东西,因为我在.Net之前先学过vb6 :)。
txtIndexData1
,txtIndexData2
.....作为控件的名称。
现在循环看起来像 [假设文本框控件直接放在表单上]
For I = 0 To LINK_BOX_SIZE - 1
Dim obj As TextBox = CType(Me.Controls("txtIndexData" & I), TextBox)
If obj.ReadOnly = False Then
CurrentScrollValue = vsbIndex.Value
vsbIndex.Value = LastScrollValue
Call txtIndexData1_LostFocus(obj, New System.EventArgs())
vsbIndex.Value = CurrentScrollValue
End If
Next I
唯一的痛点是自己命名控件,因为vb6会自动增加索引,但也有一个解决方案。只需创建一个usercontrol并直接将继承更改为文本框,然后将控件命名为txtIndexData
。现在编译你的程序。它应该出现在工具箱上。将其拖到项目中。当您创建副本时,它会自动增加到txtIndexData1
,txtIndexData2
,txtIndexData3
...等等。