Vb.net 如何确定VB6控件是否在.NET中编制了索引(控件数组)
我正在处理一个VB.NET项目,以便使用COM互操作操作操作VB6表单。VB6窗体上的某些控件已编制索引,而某些控件未编制索引,因此在没有索引的控件上调用ctl.Index失败。有没有办法确定一个控件是否被索引?我已经设法找到了一个解决方案来实现这个功能。但它并没有那么有效,因为它每次都会遍历表单上的所有控件。我似乎记得在我的脑海深处有一个VB6函数来测试控件是否是数组,但我记不起来了。我对任何感兴趣的人的功能如下,但如果可能的话,我仍然有兴趣找到一个更干净的解决方案Vb.net 如何确定VB6控件是否在.NET中编制了索引(控件数组),vb.net,vb6,com-interop,vb6-migration,Vb.net,Vb6,Com Interop,Vb6 Migration,我正在处理一个VB.NET项目,以便使用COM互操作操作操作VB6表单。VB6窗体上的某些控件已编制索引,而某些控件未编制索引,因此在没有索引的控件上调用ctl.Index失败。有没有办法确定一个控件是否被索引?我已经设法找到了一个解决方案来实现这个功能。但它并没有那么有效,因为它每次都会遍历表单上的所有控件。我似乎记得在我的脑海深处有一个VB6函数来测试控件是否是数组,但我记不起来了。我对任何感兴趣的人的功能如下,但如果可能的话,我仍然有兴趣找到一个更干净的解决方案 Private Funct
Private Function FindIndex(ByRef objCtl As Object) As Integer
For Each ctl As Object In objCtl.Parent.Controls
If objCtl.Name = ctl.Name AndAlso Not objCtl.Equals(ctl) Then
'if the object is the same name but is not the same object we can assume it is a control array
Return objCtl.Index
End If
Next
'if we get here then no controls on the form have the same name so can't be a control array
Return 0
End Function
如果有人感兴趣,以下是VB6的等效项:
Private Function FindIndex(ByRef F As Form, ByRef Ctl As Control) As Integer
Dim ctlTest As Control
For Each ctlTest In F.Controls
If (ctlTest.Name = Ctl.Name) And (Not (ctlTest Is Ctl)) Then
'if the object is the same name but is not the same object we can assume it is a control array
FindIndex = Ctl.Index
Exit Function
End If
Next
'if we get here then no controls on the form have the same name so can't be a control array
FindIndex = 0
End Function
在vb6中,您可以使用TypeName函数-控件数组将返回类型“Object”,而不是实际控件类型-如下所示:
If TypeName(ctrl) = "Object" Then
isControlArray = true
End If
我找到了一个类似于的解决方案,但它避免了在表单上循环所有控件:
Public Function IsControlArray(objCtrl As Object) As Boolean
IsControlArray = Not objCtrl.Parent.Controls(objCtrl.Name) Is objCtrl
End Function
来源:如果控制阵列只有一个成员,则上述建议的解决方案不起作用。 测试控件是否作为控件数组成员的一种简单方法是测试控件的索引属性(控件数组)。这将返回唯一标识控件数组中控件的数字。仅当控件是控件数组的一部分时可用
Private Function IsControlArray(Ctl As Control) As Boolean
On Error GoTo NotArray
IsControlArray = IsNumeric(Ctl.Index)
Exit Function
NotArray:
IsControlArray = False
End Function
我原以为iArray可以工作,但正如在VB6中一样,它不认为控件数组实际上是数组。我不认为有VB6函数来测试控件是否是控件数组的一部分。此解决方案不适用于CommandButton对象。即使在数组中,TypeName也会显示“CommandButton”。这不起作用-TypeName()返回对象的类型,不管它是否是数组的一部分。