CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2001
    Posts
    12

    help using the treeView control

    hello.
    can someone give me an example of using the treeView control?


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: help using the treeView control

    Here is a simple one.
    Start a new project. Add a Module.
    Add A textbox (Text1)at the top about 3 inches wide
    Add a Treeview (TV) directly below it
    Add two command buttons below it (cmdBegin, cmdClose)
    Paste this code into the Module


    option Explicit
    '
    public Const MAX_PATH as Long = 260
    public Const FILE_ATTRIBUTE_ARCHIVE = &H20
    public Const FILE_ATTRIBUTE_COMPRESSED = &H800
    public Const FILE_ATTRIBUTE_DIRECTORY = &H10
    public Const FILE_ATTRIBUTE_HIDDEN = &H2
    public Const FILE_ATTRIBUTE_NORMAL = &H80
    public Const FILE_ATTRIBUTE_READONLY = &H1
    public Const FILE_ATTRIBUTE_SYSTEM = &H4
    public Const FILE_ATTRIBUTE_TEMPORARY = &H100

    Type FileTime ' 8 Bytes
    dwLowDateTime as Long
    dwHighDateTime as Long
    End Type

    Type WIN32_FIND_DATA ' 318 Bytes
    dwFileAttributes as Long
    ftCreationTime as FileTime
    ftLastAccessTime as FileTime
    ftLastWriteTime as FileTime
    nFileSizeHigh as Long
    nFileSizeLow as Long
    dwReserved¯ as Long
    dwReserved1 as Long
    cFileName as string * MAX_PATH
    cAlternate as string * 14
    End Type

    public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    (byval lpFileName as string, lpFindFileData as WIN32_FIND_DATA) as Long
    public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (byval hFindFile as Long, lpFindFileData as WIN32_FIND_DATA) as Long
    public Declare Function FindClose Lib "kernel32" (byval hFindFile as Long) as Long
    '
    '
    'Paste this code into the general declaration section of the Form
    '
    '
    option Explicit

    private Sub cmdBegin_Click()
    TV.Nodes.Clear
    TV.Nodes.Add , , "Root", Left(Text1.Text, 2) ' Node One
    TV.Nodes(1).Expanded = true ' Expand it
    FillTreeview Text1.Text, "Root"
    End Sub

    private Sub cmdClose_Click()
    Unload me
    End Sub

    public Sub FillTreeview(Path as string, Parent as string, optional Recurse as Boolean = false)
    static Key as string
    Dim hwnd, WFD as WIN32_FIND_DATA
    Dim X as Integer
    Dim fn as string
    Dim idx, pth as string
    Dim MyChild as Integer
    Dim Iam as string
    If Key = "" then Key = GetQualifier
    MyChild = 1
    pth = Path ' get Folder to work on
    If Right(pth, 1) <> "\" then pth = pth & "\" ' right backslash
    hwnd = FindFirstFile(pth & "*.*", WFD) ' get first file
    If hwnd = -1 then Exit Sub ' nothing there
    fn = StripNull(WFD.cFileName) ' clear trailing nulls
    Do ' Find Children
    If fn <> "." And fn <> ".." then
    If (WFD.dwFileAttributes And vbDirectory) = vbDirectory then
    TV.Nodes.Add Parent, tvwChild, Key & "." & CStr(MyChild), fn
    TV.Nodes(TV.Nodes.Count).Tag = pth & fn
    If Recurse then
    FillTreeview pth & fn, Key & "." & CStr(MyChild), Recurse ' recurse
    else
    TV.Nodes.Add Key & "." & CStr(MyChild), tvwChild, Key & "." & CStr(MyChild) & "_Fake", "_Fake"
    End If
    MyChild = MyChild + 1 ' next Childs Identifire
    End If
    End If
    X = FindNextFile(hwnd, WFD)
    fn = StripNull(WFD.cFileName)
    If (WFD.dwFileAttributes And vbDirectory) = vbDirectory then Key = GetQualifier
    Loop Until X = 0
    set hwnd = nothing
    End Sub

    public Function StripNull(byval WhatStr as string) as string
    ' Remove nulls from end of string
    Dim pos as Integer
    pos = InStr(WhatStr, Chr$(0))
    If pos > 0 then
    StripNull = Left$(WhatStr, pos - 1)
    else
    StripNull = WhatStr
    End If
    End Function

    public Function GetFileName(P as string)
    Dim X
    X = InStrRev(P, "\", -1, vbTextCompare)
    GetFileName = mid(P, X + 1)
    End Function

    public Function GetQualifier()
    ' Routine returns a twl character string like AA or AB or AC or BA or BB or BC etc
    static Q1 as Integer
    static Q2 as Integer
    If Q1 = 0 then Q1 = 65
    If Q2 = 0 then Q2 = 64
    Q2 = Q2 + 1
    If Q2 = Asc("Z") then
    Q1 = Q1 + 1
    Q2 = 65
    End If
    GetQualifier = Chr(Q1) & Chr(Q2)
    End Function

    private Sub Form_Load()
    Text1.Text = "C:\Program Files"
    End Sub

    private Sub TV_Expand(byval Node as MSComctlLib.Node)
    If Node.Child = "_Fake" then
    TV.Nodes.Remove Node.Child.Key
    FillTreeview Node.Tag, Node.Key, true
    End If
    End Sub





    John G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured