Vb6 Visual Basic 6中的控件属性

Vb6 Visual Basic 6中的控件属性,vb6,controls,properties,Vb6,Controls,Properties,有没有办法在循环中请求控件属性 我需要这样的东西: For each p in control.properties if p = "Value" then msgbox "I Have Value Property" elseif p = "Caption" then msgbox "I Have Caption Property" end if next If control.Value Is Not Nothing Then

有没有办法在循环中请求控件属性

我需要这样的东西:

For each p in control.properties
    if p = "Value" then
        msgbox "I Have Value Property"
    elseif p = "Caption" then
        msgbox "I Have Caption Property"
    end if 
next
If control.Value Is Not Nothing Then
   msgbox "I Have Value Property"
Else If control.Caption Is Not Nothing Then
   msgbox "I Have Caption Property"

它可以通过某种方式实现吗?

我不确定您希望实现什么,但我非常确定VB6不支持您所说的。您可以尝试以下方法:

For each p in control.properties
    if p = "Value" then
        msgbox "I Have Value Property"
    elseif p = "Caption" then
        msgbox "I Have Caption Property"
    end if 
next
If control.Value Is Not Nothing Then
   msgbox "I Have Value Property"
Else If control.Caption Is Not Nothing Then
   msgbox "I Have Caption Property"

看看这是否完成了您希望完成的任务。

我不确定您希望完成什么,但我非常确定VB6不支持您所说的内容。您可以尝试以下方法:

For each p in control.properties
    if p = "Value" then
        msgbox "I Have Value Property"
    elseif p = "Caption" then
        msgbox "I Have Caption Property"
    end if 
next
If control.Value Is Not Nothing Then
   msgbox "I Have Value Property"
Else If control.Caption Is Not Nothing Then
   msgbox "I Have Caption Property"

查看这是否完成了您希望执行的操作。

在专家交换上找到了此代码。添加对类型库信息的引用

Public Enum EPType
    ReadableProperties = 2
    WriteableProperties = 4
End Enum

Public Function EnumerateProperties(pObject As Object, pType As EPType) As Variant
    Dim rArray() As String
    Dim iVal As Long
    Dim TypeLib As TLI.InterfaceInfo
    Dim Prop As TLI.MemberInfo
    On Error Resume Next
    ReDim rArray(0) As String
    Set TypeLib = TLI.InterfaceInfoFromObject(pObject)
    For Each Prop In TypeLib.Members
        If Prop.InvokeKind = pType Then
            iVal = UBound(rArray)
            rArray(iVal) = UCase$(Prop.Name)
            ReDim Preserve rArray(iVal + 1) As String
        End If
    Next
    ReDim Preserve rArray(UBound(rArray) - 1) As String
    EnumerateProperties = rArray
End Function
您可以要求提供可读或可写属性的列表

另外,询问是否存在特定的财产

Public Function DoesPropertyExist(pObject As Object, ByVal _
    PropertyName As String, pType As EPType) As Boolean
    Dim Item As Variant
    PropertyName = UCase$(PropertyName)
    For Each Item In EnumerateProperties(pObject, pType)
        If Item = PropertyName Then
            DoesPropertyExist = True
            Exit For
        End If
    Next
End Function

在专家交换中找到此代码。添加对类型库信息的引用

Public Enum EPType
    ReadableProperties = 2
    WriteableProperties = 4
End Enum

Public Function EnumerateProperties(pObject As Object, pType As EPType) As Variant
    Dim rArray() As String
    Dim iVal As Long
    Dim TypeLib As TLI.InterfaceInfo
    Dim Prop As TLI.MemberInfo
    On Error Resume Next
    ReDim rArray(0) As String
    Set TypeLib = TLI.InterfaceInfoFromObject(pObject)
    For Each Prop In TypeLib.Members
        If Prop.InvokeKind = pType Then
            iVal = UBound(rArray)
            rArray(iVal) = UCase$(Prop.Name)
            ReDim Preserve rArray(iVal + 1) As String
        End If
    Next
    ReDim Preserve rArray(UBound(rArray) - 1) As String
    EnumerateProperties = rArray
End Function
您可以要求提供可读或可写属性的列表

另外,询问是否存在特定的财产

Public Function DoesPropertyExist(pObject As Object, ByVal _
    PropertyName As String, pType As EPType) As Boolean
    Dim Item As Variant
    PropertyName = UCase$(PropertyName)
    For Each Item In EnumerateProperties(pObject, pType)
        If Item = PropertyName Then
            DoesPropertyExist = True
            Exit For
        End If
    Next
End Function

比纳对你提出的问题给予了极好的回答

我在猜你可能想做什么。也许您试图从控件获取“文本”,但在运行时不知道控件的类型。你可以考虑这样的事情,它会尝试多个硬编码的属性名称,直到某个东西起作用。
Function sGetSomeText(ctl As Object) As String 
  On Error Resume Next 
  sGetSomeText = ctl.Text
  If Err = 0 Then Exit Function 
  sGetSomeText = ctl.Caption
  If Err = 0 Then Exit Function 
  sGetSomeText = ctl.Value
  If Err = 0 Then Exit Function
  sGetSomeText = "" 'Nothing worked '
End Function 
另一种方法是在运行时检查控件的类型。你可以用


然后,您可以切换到特定控件类型的代码,这些控件类型肯定具有
Text
属性等。

Beaner对您提出的问题给出了极好的回答

我在猜你可能想做什么。也许您试图从控件获取“文本”,但在运行时不知道控件的类型。你可以考虑这样的事情,它会尝试多个硬编码的属性名称,直到某个东西起作用。
Function sGetSomeText(ctl As Object) As String 
  On Error Resume Next 
  sGetSomeText = ctl.Text
  If Err = 0 Then Exit Function 
  sGetSomeText = ctl.Caption
  If Err = 0 Then Exit Function 
  sGetSomeText = ctl.Value
  If Err = 0 Then Exit Function
  sGetSomeText = "" 'Nothing worked '
End Function 
另一种方法是在运行时检查控件的类型。你可以用


然后,您可以切换到特定控件类型的代码,这些控件类型肯定具有
Text
属性等。

作为旁注,VB.NET的一个主要优点是所有控件都具有
.Text
属性,而不是
Caption
Value
。作为旁注,VB.NET的一个主要优点是,所有控件都有
.Text
属性,而不是
Caption
Value
。实际上,我甚至不确定它是否可以编译。我仍然觉得它比“对不起,你完蛋了”更有用。事实上,我甚至不确定它是否可以编译。我仍然觉得这比“对不起,你完蛋了”更有帮助。这里还有一些很好的代码用于+1。这里还有一些很好的代码