Working with a treeview..(1st time)

|2eM!x

Member
Joined
Feb 19, 2006
Messages
5
Programming Experience
1-3
Well for me im just kind of experimenting with the treeview and with the process class, Now it was all working grand until I tried to go 3 nodes in etc etc..

Can anyone help me, I need to have it add all the classes+hwnds of the children into the Classes node of each parent node

VB.NET:
For z = 0 To bStr.GetUpperBound(0) - 1
                        .Nodes(i).Nodes(0).Nodes.Add(bStr(z))
                    Next

Is where it goes wrong..

In form1
VB.NET:
Option Strict On
Option Explicit On
Imports System.text

Public Class Form1
    Private Structure tProc
        Dim Name As String
        Dim MainWindowTitle As String
        Dim Handle As String
        Dim vMemory As String
    End Structure
    Dim tProcess As tProc
    Dim sBuild As New StringBuilder(256)
    Dim ChildHandles As String
    Dim proc As New EnumWindProc(AddressOf EnumChild)

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim tProc() As Process = Process.GetProcesses
        Dim i As Int32, z As Int32, bStr() As String
        With TreeView1
            For i = 0 To tProc.GetUpperBound(0)
                tProcess.Name = tProc(i).ProcessName
                tProcess.MainWindowTitle = tProc(i).MainWindowTitle
                tProcess.Handle = tProc(i).MainWindowHandle.ToString
                tProcess.vmemory = tProc(i).VirtualMemorySize64.ToString
                .Nodes.Add(tProcess.Name)
                .Nodes(i).Nodes.Add("Virtual Memory:" & tProcess.vMemory)
                If Convert.ToInt32(tProcess.Handle) <> 0 Then
                    EnumChildWindows(CType(tProcess.Handle, IntPtr), proc, IntPtr.Zero)
                    bStr = ChildHandles.Split(","c)
                    If bStr.GetUpperBound(0) > 0 Then .Nodes(i).Nodes.Add("Classes")
                    For z = 0 To bStr.GetUpperBound(0) - 1
                        .Nodes(i).Nodes(0).Nodes.Add(bStr(z))
                    Next
                    ChildHandles = String.Empty
                End If
                If tProcess.MainWindowTitle <> String.Empty Then
                    .Nodes(i).Nodes.Add("WindowTitle:" & tProcess.MainWindowTitle)
                Else
                    .Nodes(i).Text = .Nodes(i).Text.ToUpper
                End If
                If Convert.ToInt32(tProcess.Handle) <> 0 Then
                    .Nodes(i).Nodes.Add("Handle:" & tProcess.Handle)
                Else
                    .Nodes(i).Text = .Nodes(i).Text.ToUpper
                End If
            Next
            .Sort()
        End With
        Timer1.Stop()
    End Sub

    Private Function EnumChild(ByVal hWnd As Int32, ByVal lParam As Int32) As Boolean
        GetClassName(hWnd, sBuild, sBuild.Capacity)
        ChildHandles += (sBuild.ToString & "-" & hWnd & ",")
        EnumChild = True
    End Function

End Class
VB.NET:
Imports system.text
Module Module1

    Declare Function EnumChildWindows Lib "user32" ( _
                ByVal hWnd As IntPtr, _
                ByVal lpEnumFunc As EnumWindProc, _
                ByRef lParam As IntPtr) As Int32

    Delegate Function EnumChildWindProc( _
                ByVal hWnd As Int32, _
                ByVal lParam As Int32) As Boolean

    Delegate Function EnumWindProc( _
                  ByVal hWnd As Int32, _
                  ByVal lParam As Int32) As Boolean

    Public Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" ( _
                ByVal hwnd As Int32, _
                ByVal lpClassName As StringBuilder, _
                ByVal nMaxCount As Int32) As Int32

End Module
 
I don't get any error running this actually, but I have one tip that will make this easier to read and less errorprone.
When you add nodes and move to deeper levels catch the TreeNode in a variable for next access.
Here is a cut-out of your code, I have added two TreeNode variables procnode and classnode, see how it's used:
VB.NET:
[SIZE=2][COLOR=#0000ff][COLOR=black]Dim[/COLOR][/COLOR][/SIZE][COLOR=black][SIZE=2] procnode, classnode [/SIZE][SIZE=2]As[/SIZE][SIZE=2] TreeNode
[/SIZE][/COLOR]With TreeView1
'
'[SIZE=2]...[/SIZE]
'
procnode = .Nodes.Add(tProcess.Name)
procnode.Nodes.Add("Virtual Memory:" & tProcess.vMemory)
If Convert.ToInt32(tProcess.Handle) <> 0 Then
  EnumChildWindows(CType(tProcess.Handle, IntPtr), proc, IntPtr.Zero)
  bStr = ChildHandles.Split(","c)
  If bStr.GetUpperBound(0) > 0 Then
    classnode = procnode.Nodes.Add("Classes")
    For z = 0 To bStr.GetUpperBound(0) - 1
      classnode.Nodes.Add(bStr(z))
    Next
  End If
  ChildHandles = String.Empty
End If
'
'[SIZE=2]...[/SIZE]
'
End With
better than .Nodes(i).Nodes(0).Nodes.Add(bStr(z)) is it not?
 
That works AMAZING! I love this forum!

Is there some way to rep you (like on another vb site) or is my thanks good enough? :D

Just an FYI for further info, this is how I finished it:

VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim tProc() As Process = Process.GetProcesses
        Dim ProcNode, System, Current_User, ClassNode As TreeNode
        Dim i As Int32, z As Int32, bStr() As String
        With TreeView1
            System = .Nodes.Add("System")
            Current_User = .Nodes.Add("Current_User")
            For i = 0 To tProc.GetUpperBound(0)
                tProcess.Name = tProc(i).ProcessName
                tProcess.MainWindowTitle = tProc(i).MainWindowTitle
                tProcess.Handle = tProc(i).MainWindowHandle.ToString
                tProcess.vMemory = tProc(i).VirtualMemorySize64.ToString
                If Convert.ToInt32(tProcess.Handle) <> 0 Then
                    ProcNode = Current_User.Nodes.Add(tProcess.Name.ToString)
                    ClassNode = ProcNode.Nodes.Add("Classes")
                    EnumChildWindows(CType(tProcess.Handle, IntPtr), proc, IntPtr.Zero)
                    bStr = ChildHandles.Split(","c)
                    For z = 0 To bStr.GetUpperBound(0) - 1
                        ClassNode.Nodes.Add(bStr(z))
                    Next
                    ChildHandles = String.Empty
                Else
                    System.Nodes.Add(tProcess.Name.ToString)
                End If
            Next
            .Sort()
        End With
        Timer1.Stop()
    End Sub

Well its actually not close to being done, but i rewrote the code to make me happy :)
 
Last edited:
Back
Top