indexed properties, methods, events of objects

Hi All,

   I would like to do something similar to the object browser in VB.  I want to view all of the properties, methods, and events of an object.  Is there a way to scroll through each member of an object?  Are they enumerated somehow?

   The easiest way would be to make a FOR LOOP and print each item to a text file.

   Any ideas?

Thanks
DoughBoy
DoughBoyAsked:
Who is Participating?
 
mcriderConnect With a Mentor Commented:
Here you go...

Before you use this code, you need to add the "TypeLib" reference to your program.  To do this, click "Project->References..." then select "TypeLib Information" from the list.

Add the following code to a MODULE in your program:

    Public Enum EnumObjType
        Functions = 1
        ReadableProperties = 2
        WriteableProperties = 4
    End Enum
    Function EnumObject(ObjName As Object, ObjType As EnumObjType) As Variant
        Dim lArray() As String
        Dim TypeLib As TLI.InterfaceInfo
        Dim Prop As TLI.MemberInfo
        Dim iVal As Long
        On Error Resume Next
        iVal = 0
        ReDim lArray(0) As String
        Set TypeLib = TLI.InterfaceInfoFromObject(ObjName)
        For Each Prop In TypeLib.Members
            Select Case Prop.InvokeKind
                Case ObjType
                    ReDim Preserve lArray(iVal) As String
                    lArray(iVal) = Prop.Name
                    iVal = iVal + 1
            End Select
        Next
        On Error Resume Next
        Err = 0
        ReDim Preserve lArray(iVal - 1) As String
        If Err = 0 Then
            EnumObject = lArray
        Else
            EnumObject = ""
        End If
    End Function



Then you can use the EnumObject function to list all of the subroutines and properties of an object.  Here is an example to list the subroutines of the commandbutton object named "Command1"...


    Dim EnumList As Variant
    Dim Element As Variant
   
    On Error Resume Next
    EnumList = EnumObject(Command1, Functions)
    If IsArray(EnumList) Then
        For Each Element In EnumList
            Debug.Print "GOT:" & CStr(Element)
        Next
    End If




To list all of the readable properties of the same object, do this:




    Dim EnumList As Variant
    Dim Element As Variant
   
    On Error Resume Next
    EnumList = EnumObject(Command1, ReadableProperties)
    If IsArray(EnumList) Then
        For Each Element In EnumList
            Debug.Print "GOT:" & CStr(Element)
        Next
    End If



To get the values of the properties, you need to do a "Select Case" on each element that comes back... Something like this:

    Function GetValue(ObjName As Object, PropertyName As String) As Variant
        On Error Resume Next
        'YOU NEED A CASE STATEMENT FOR EACH PROPERTY YOU ARE
        'GOING TO RETURN A VALUE FOR. CURRENTLY THIS RETURNS
        'THE PROPERTY VALUES FOR:
        '
        '    TEXT, CAPTION, TOP, LEFT, HEIGHT, and WIDTH
        '
        Select Case UCase$(PropertyName)
            Case "TEXT"
                GetValue = ObjName.Text
            Case "CAPTION"
                GetValue = ObjName.Caption
            Case "TOP"
                GetValue = ObjName.Top
            Case "LEFT"
                GetValue = ObjName.Left
            Case "HEIGHT"
                GetValue = ObjName.Height
            Case "WIDTH"
                GetValue = ObjName.Width
        End Select
    End Function


By the way, I think this is worth more than 50 points...


Cheers!
0
 
tcornettCommented:
DoughBoy,

Say you have an object named MyObj that has a member called Properties which holds an unknown number of properties for MyObj.  You would normally access property x by using MyObj.Properties(x).  If you want to output an entire list of properties, use the a FOR EACH LOOP.  See the below for an example.  Hope this helps!!

- Tom

'Example of listing properties for an object
dim MyObj as Object
dim MyProp as Property

'Do whatever to initialize/set your object

for each MyProp in MyObj.Properties
   msgbox MyProp.Name
next MyProp

The above code will display each properties name in a message box.  I hope this is what you are looking for.
0
 
DoughBoyAuthor Commented:
Hi Tom,
  I appreciate your help.  That gets me started.  However,  I also need to know the events and methods as well.  When you are in the VB IDE and suppose you have a textbox named text1,  in the code you type "text1."  As soon as you press the "."  a list of all the proprties as well as methods are listed.  Is there a way to save this?  Even that only gets me 2/3 of the way there.  

Thanks
DoughBoy

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DoughBoyAuthor Commented:
Adjusted points to 100
0
 
DoughBoyAuthor Commented:
Thanks very much!
Works like a charm.
100 pts!
0
 
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!
0
All Courses

From novice to tech pro — start learning today.