Cara Membuat Object Browser Sederhana di Form VB6

Cara Membuat Object Browser Sederhana di Form VB6 - Pada pertemuan kali ini tip dan trik visual basic akan memberikan sebuah tip yaitu bagaimana membuat object browser dengan menggunakan Visual Basic 6.0 Object, sebelum kita membahas tip ini kita harus tau apa itu Object Browser.

Object Browser adalah alat bantu dalam bentuk kotak dialog untuk melacak obyek-obyek dalam sebuah database. Selain fungsi navigasi, Object Browser memungkinkan pembuatan script, mengeksekusi stored procedure, dan mengakses tabel atau view.Untuk mengenal contoh dari object browser tidak perlu jauh-jauh cukup di komputer Anda saat ini yaitu dengan cara mengklik bagian windows explorer,distulah semua file dan folder kita berada agar lebih mudah diakses.

Untuk lebih jelasnya tentang object browser sebaiknya Anda ikuti tip berikut agar Anda tau seperti apa object browser itu.Berikut adalah langkah-langkah dalam pembuatannya :

Cara Membuat Object Browser Sederhana di VB6

1. Buka Form VB6 Anda,ganti Name Propertiesnya menjadi : frmTree
2.Tambahkan Komponen dengan menekan CTL+T Cek List Microsoft Windows Common  Controls 6.0
3. Tambahkan komponen TreeView, StatusBar dan 1 Commanbutton di dalam Form. Atur priperties Name masing-masing :

   TreeView Name               : treeList
   StatusBar Name               : statBar
   CommandButton1 Name : cmdRun

4. Kemudian Desain Form seperti gambar dibawah ini :

Desain Form


5. Setelah mendesain Form,Tambahkan 1 Modules dengan cara Project > Add Module, Name Properties : modMain
6. kemudian Ketik kode dibawah ini di module :

    Public Sub Main()
    frmTree.Show
    End Sub

7. Langkah selanjutnya Tambahkan 2 Class Module dengan cara Project > Add Class Module, beriName Propertienya masing-masing dengan Name : clsTest dan clsTest2
8. Ketik kode dibawah ini di Class Modules clsTest :

     Public intTest As Integer
     Public strTest As String
     Public objTest As clsTest2
     Public objSelfRef As clsTest

Public Sub SubroutineTest()
    MsgBox "Test"
End Sub

Public Function FuncTest(strTest As String) As String
 
    Me.strTest = strTest & " tested"
    Me.intTest = Me.intTest + 1
 
End Function

9. Kemudian Ketik juga Kode dibawah ini di Class Modules clsTest2 :

     Public Whatever As Double


10. Sekarang kembali Form Desain, pada form Desain buka jendela kode lalu ketik kode dibawah ini :

Dim ClassNames() As String
Dim mobjClass As clsTest
Private Sub cmdRun_Click()
    
    Dim n As Long
    Dim i As Long
    Dim ClassInfo As InterfaceInfo
    Dim ClassMember As MemberInfo
    Dim alParams() As Variant
    Dim hr As Variant
    
 
    'grab class info
    Set ClassInfo = tli.InterfaceInfoFromObject(mobjClass)
    Set ClassMember = ClassInfo.GetMember(ClassNames(treeList.SelectedItem.Index))
 
    'here we get the number of parameters that the function requires
    n = ClassMember.Parameters.Count - 1
 
    'if the function has parameters, we will loop through them and prompt for each one.
    If n >= 0 Then
        ReDim alParams(n)
     
        'when using InvokeHookArray, parameters are passed in reverse order, so we set up the For loop accordingly.
        For i = n To 0 Step -1
            alParams(i) = InputBox("Enter a value for parameter " & ClassMember.Parameters((n - i) + 1).Name, ClassMember.Name & " parameters")
        Next i
     
        'InvokeHookArray is extremely useful when you don't know how many parameters you need to pass until runtime.
        hr = InvokeHookArray(mobjClass, ClassMember.MemberId, INVOKE_FUNC, alParams)
    Else
        'InvokeHook let's us call without any parameters more simply than InvokeHookArray.
        hr = InvokeHook(mobjClass, ClassMember.MemberId, INVOKE_FUNC)
    End If
 
End Sub

Private Sub Form_Load()
     
    Set mobjClass = New clsTest
    BuildClassTree mobjClass
 
End Sub
Sub BuildClassTree(objClass As Object)
    Dim ClassInfo As InterfaceInfo
    Set ClassInfo = tli.InterfaceInfoFromObject(objClass)
        AddClassNodes ClassInfo.Members, ClassInfo.Name, -1
 
End Sub

Sub AddClassNodes(ClassMembers As Members, strClassName, lngParent As Long)
 
    Dim strKey As String
    Dim newNode As Node
    Dim FilteredMembers As SearchResults
    Dim ClassMember As MemberInfo
    Dim i As Long
 
 
    'this builds the heading nodes
    If lngParent >= 0 Then
        treeList.Nodes.Add lngParent, tvwChild, "Functions" & CStr(lngParent), "Functions"
        treeList.Nodes.Add lngParent, tvwChild, "Objects" & CStr(lngParent), "Objects"
        treeList.Nodes.Add lngParent, tvwChild, "Variables" & CStr(lngParent), "Variables"
    Else
        treeList.Nodes.Add , , "Functions" & CStr(lngParent), "Functions"
        treeList.Nodes.Add , , "Objects" & CStr(lngParent), "Objects"
        treeList.Nodes.Add , , "Variables" & CStr(lngParent), "Variables"
    End If
 
    'if there is no parent, this is the first loop - initialize the ClassNames array
    If lngParent = -1 Then
        ReDim Preserve ClassNames(-1 To 0)
        ClassNames(-1) = strClassName
    End If
 
    'we need the filtered members in order to eliminate COM functions that VB will add in automatically
    'once we compile the exe.
    Set FilteredMembers = ClassMembers.GetFilteredMembers
 
    For i = 1 To FilteredMembers.Count
     
        'InvokeKinds stores the type of member this is. We'll check for particular flags to determine
        'if it falls into the category of function/sub, variable property, or object property.
        If FilteredMembers(i).InvokeKinds And INVOKE_FUNC Then
            strKey = "Functions" & CStr(lngParent)
        ElseIf FilteredMembers(i).InvokeKinds And INVOKE_PROPERTYPUT Then
            strKey = "Variables" & CStr(lngParent)
        ElseIf FilteredMembers(i).InvokeKinds And INVOKE_PROPERTYPUTREF Then
            strKey = "Objects" & CStr(lngParent)
        Else
            'catch-all
            GoTo NextMember
        End If
     
        'this is our function to get the unfiltered version of this member
        Set ClassMember = GetMemberByID(ClassMembers, FilteredMembers(i).MemberId)
     
        'add the member to the tree
        Set newNode = treeList.Nodes.Add(strKey, tvwChild, , ClassMember.Name)
     
        If strKey = "Objects" & CStr(lngParent) Then
            'here we compare the type of the object property with the type of its parent
            'if they match, don't build a subtree for this one, or it will infinitely loop within itself.
            'instead, bold the node to signal a self-reference.
            If ClassNames(lngParent) = ClassMember.ReturnType.TypeInfo.Name Then
                newNode.Bold = True
            Else
         
                'while the top-level class has to be running, we can grab info on all child classes (properties
                'that are objects) whether they are running or not, by using the ReturnType.TypeInfo property.
                AddClassNodes ClassMember.ReturnType.TypeInfo.Members, ClassMember.ReturnType.TypeInfo.Name, newNode.Index
             
                'store the name of the class that this object property is defined as
                'this is used for comparison above
                If newNode.Index > UBound(ClassNames) Then ReDim Preserve ClassNames(-1 To newNode.Index)
                ClassNames(newNode.Index) = ClassMember.ReturnType.TypeInfo.Name
            End If
         
            'we'll use the Tag property of the Nodes to store the return types of the properties and functions.
            'we use Mid to take the underscore off the front of custom class names
            newNode.Tag = Mid(ClassMember.ReturnType.TypeInfo.Name, 2)
        Else
            'store the name of the member for later retrieval
            If newNode.Index > UBound(ClassNames) Then ReDim Preserve ClassNames(-1 To newNode.Index)
            ClassNames(newNode.Index) = ClassMember.Name
         
            'store return type
            newNode.Tag = TypeName(ClassMember.ReturnType.TypedVariant)
        End If
     
NextMember:
    Next i
 
End Sub

Function GetMemberByID(objMembers As Members, memID As Long) As MemberInfo
 
    Dim i As Long
 
 
 
    For i = 1 To objMembers.Count
        If objMembers(i).MemberId = memID Then
            Set GetMemberByID = objMembers(i)
            Exit For
        End If
    Next i
 
End Function

Private Sub Form_Resize()
 
    treeList.Width = Me.ScaleWidth
    treeList.Height = Me.ScaleHeight - statBar.Height
    cmdRun.Top = Me.ScaleHeight - cmdRun.Height
 
End Sub

Private Sub treeList_NodeClick(ByVal Node As MSComctlLib.Node)
 
    statBar.Panels(1).Text = "Type: " & Node.Tag
 
    If Not Node.Parent Is Nothing Then
 
        If Node.Parent.Text = "Functions" And Node.Parent.Parent Is Nothing Then
            cmdRun.Enabled = True
        Else
            cmdRun.Enabled = False
        End If
     
        If Node.Parent.Text = "Variables" And Node.Parent.Parent Is Nothing Then
             statBar.Panels(2).Text = "Value: " & CStr(InvokeHook(mobjClass, ClassNames(Node.Index), INVOKE_PROPERTYGET))
        Else
            statBar.Panels(2).Text = ""
        End If
     
    End If
 
End Sub

11. Simpan hasil pekerjaan Anda dan jalankan program.

Demikian tip cara Cara Membuat Object Browser Sederhana di Form VB6. Selamat mencoba, semoga berhasil........

No comments: