Control Properties in Visual Basic 6

Tag: vb6 , controls , properties Author: flyinwar Date: 2009-11-18

Is there a way to ask for a control property in a loop??

I need somethig like this:

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

It could be done somehow?

As a side note, one of the major advantages of VB.NET is that all controls have a .Text property instead of Caption or Value.

Best Answer

Found this code on Experts Exchange. Add a reference to TypeLib Information.

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

You can ask for a list of the readable, or writeable properties.

Bonus, ask if a specific property exists.

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

comments:

+1. There's also some nice code working with this here

Other Answer1

Beaner has given an excellent direct answer to the question you have asked.

I'm guessing what you might be trying to do. Perhaps you're trying to get the "text" from a control but you don't know the type of the control at runtime. You could consider something like this, which tries a number of hard-coded property names in turn until something works.

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

Another approach would be to check the type of the control at runtime. You can use

Then you could switch to code for specific control types that definitely have the Text property, etc.

Other Answer2

I don't think VB6 support reflection

comments:

VB6 does support reflection on public types, see Beaner's answer.

Other Answer3

I'm not sure what you're hoping to accomplish, but I'm pretty sure VB6 does not support what you're talking about. You could try something like this:

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"

See if that accomplishes what you're looking to do.

comments:

that would crash
Actually, I wasn't even sure it would compile. I still found it more helpful than "Sorry, you're screwed."