如何查明 VB6 控件是否在 .NET 中建立索引(控件数组)

发布于 2024-10-08 02:13:36 字数 122 浏览 6 评论 0原文

我正在开发一个 VB.NET 项目,以使用 COM Interop 操作 VB6 表单。我的 VB6 表单上的某些控件已建立索引,有些则没有,因此对那些没有索引的控件调用 ctl.Index 会失败。有没有办法确定控件是否已索引?

I working on a VB.NET project to manipulate a VB6 form using COM Interop. Some of the controls on my VB6 form are indexed and some not, so calling ctl.Index fails on those that have no index. Is there a way to work out if a control is indexed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天荒地未老 2024-10-15 02:13:36

我已经设法用刀叉解决方案来使其发挥作用。但它的效率并不高,因为它每次都会遍历表单上的所有控件。我似乎记得在我的脑海里有一个VB6函数可以测试一个控件是否是一个数组,但我不记得了。对于任何感兴趣的人来说,我的功能如下,但如果可能的话,我仍然有兴趣找到一个更干净的解决方案?

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

I have managed to knife and fork a solution to get this to work. But it isn't that efficient as it iterates through all controls on the form each time. I seem to remember at the back of my mind there is a VB6 function to test whether a control is an array but I can't recall it. My function for anyone who is interested is below but I would still be interested to find a cleaner solution to this if possible?

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

The Following is the VB6 Equivalent if anyone is interested:

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
最舍不得你 2024-10-15 02:13:36

我找到了一个类似于 @Matt Wilko 的解决方案,但它避免了循环遍历形式:

Public Function IsControlArray(objCtrl As Object) As Boolean
    IsControlArray = Not objCtrl.Parent.Controls(objCtrl.Name) Is objCtrl
End Function

来源:http://www.vbforums.com/showthread.php?536960-RESOLVED-how-can-i-see-if-the-object-is-array-or-not

I found a solution similar to that of @Matt Wilko, but it avoids having to loop through all the controls on the form:

Public Function IsControlArray(objCtrl As Object) As Boolean
    IsControlArray = Not objCtrl.Parent.Controls(objCtrl.Name) Is objCtrl
End Function

Source : http://www.vbforums.com/showthread.php?536960-RESOLVED-how-can-i-see-if-the-object-is-array-or-not

苍白女子 2024-10-15 02:13:36

在 vb6 中,您可以使用 TypeName 函数 - 控件数组将返回类型“Object”,而不是实际的控件类型 - 如下所示:

If TypeName(ctrl) = "Object" Then
  isControlArray = true
End If

In vb6 you can use the TypeName function - Control arrays will return type "Object", not the actual control type - like so:

If TypeName(ctrl) = "Object" Then
  isControlArray = true
End If
来世叙缘 2024-10-15 02:13:36

如果控制数组只有一个成员,则上述解决方案不起作用。
测试控件是否作为控件数组成员的一个简单方法是测试控件的索引属性(控件数组)。这返回唯一标识控件数组中的控件的数字。仅当控件是控件数组的一部分时才可用。

Private Function IsControlArray(Ctl As Control) As Boolean
    On Error GoTo NotArray
    IsControlArray = IsNumeric(Ctl.Index)
    Exit Function
NotArray:
    IsControlArray = False
End Function

The solutions proposed above do not work if there is only one member of the control array.
A simple way to test if a control as a member of a control array is to test for the control's Index Property (Control Array). This Returns the number that uniquely identifies a control in a control array. Available only if the control is part of a control array.

Private Function IsControlArray(Ctl As Control) As Boolean
    On Error GoTo NotArray
    IsControlArray = IsNumeric(Ctl.Index)
    Exit Function
NotArray:
    IsControlArray = False
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文