Results 1 to 9 of 9

Thread: basic treeview

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Question basic treeview

    hi all
    im putting together a treeview of my video collection. and im wanting to add all the information about the video under the title. can anyone help

    i have never used treeview before i normaly use listview.
    i have manged to add the titles using the code below

    --
    im wanting to add under the title a tree 3 deep, so that i can add all the info of the video
    Code:
    Dim I As Integer   ' Declare a counter variable.
    For I = 1 To 4
       Set nodX = TreeView1.Nodes.Add(, , , "title-- " & CStr(I))'this is out of msdn help
    
    Next I
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: basic treeview

    Here is a sample I threw together quickly if you have any questions just ask:

    Code:
    Private Sub Command1_Click()
    Dim tvwItem As MSComctlLib.Node
    Dim a As Long, b As Long
    
    
        With TreeView1
            .Appearance = cc3D
            .LineStyle = tvwTreeLines
            
            With .Nodes
                Set tvwItem = .Add(, , "Root", "Root")
                For a = 1 To 10
                    Set tvwItem = .Add("Root", tvwChild, "Item " & a, "Item " & a)
                    
                    For b = 1 To 5
                        Set tvwItem = .Add("Item " & a, tvwChild, a & "SubItem " & b, "SubItem " & b)
                    Next b
                Next a
            
            
            End With
        
        For Each tvwItem In .Nodes
            tvwItem.Expanded = True
        Next
        
            .Nodes.Item("Root").Selected = True
        
        End With
        
    Set tvwItem = Nothing
    End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: basic treeview

    A little more detial:

    vb Code:
    1. Private Sub Command1_Click()
    2. Dim tvwItem As MSComctlLib.Node, tvwSubItem As MSComctlLib.Node
    3. Dim a As Long, b As Long
    4.  
    5.     With TreeView1
    6.         .Appearance = cc3D
    7.         .Checkboxes = True
    8.         .LineStyle = tvwTreeLines
    9.        
    10.         With .Nodes
    11.             Set tvwItem = .Add(, , "Root")
    12.             tvwItem.Text = "Root"
    13.             tvwItem.Tag = "Expand"
    14.            
    15.             For a = 1 To 10
    16.                 Set tvwItem = .Add("Root", tvwChild, "Item " & a)
    17.                 tvwItem.Text = "Item " & a
    18.                 tvwItem.Bold = (a Mod 3)
    19.                 tvwItem.Checked = (a Mod 2)
    20. '                tvwItem.Image = "ImageName" ' If you have an Image Box
    21.                 tvwItem.ForeColor = (25 * a)
    22.                 tvwItem.Tag = "Expand"
    23.                
    24.                 For b = 1 To 5
    25.                     Set tvwSubItem = .Add(tvwItem.Key, tvwChild, a & "SubItem " & b)
    26.                     tvwSubItem.Text = "SubItem " & b
    27.                 Next b
    28.             Next a
    29.         End With
    30.    
    31.         For Each tvwItem In .Nodes
    32.             tvwItem.Expanded = (tvwItem.Text = "Root") ' Will expand all children root nodes
    33.         Next
    34.        
    35.         .Nodes.Item("Root").Selected = True ' Will ensure that root node is visible
    36.    
    37.     End With
    38.    
    39. Set tvwItem = Nothing
    40. Set tvwSubItem = Nothing
    41. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Thumbs up Re: basic treeview

    hi Mark Gambo
    thats spot on got me started, thanks i understand now how it work much better than listview more info can be stored in the list

    one other thing im wanting to be able to get information from the tree for example under title of my video i have added all the information about the video(number,Age). after selecting the video title all the information about the video(number,Age) is displayed in labels. i have tryed below for title witch work ok(i need the info under that title).

    thanks again mark for your help

    Code:
    Label1.Caption = TreeView1.SelectedItem
    programming pc: laptop. running xp pro and vb.net..
    media centre xp pc: dual core 6000,4gb memory, 1tb harddrive
    mainserver: server 2008, 8gb memory, amd e-350 dual core, 6tb harddrive and local web host
    atomvault 1: server 2008, atom 330 with 4 gb memory, 35TB hardrive space for videos and music..
    backup pc : xp, 4gb, atom 330, 10tb harddrive(web, pictures, music, databases)
    2 x video backup: atom 330, 2gb, 18tb harddrive
    everything on remote login..
    check out my builds http://AtomVaults.yolasite.com

    learning vb.net and php + mysql - got most of the basics now.

    I WISH THERE WAS MORE TIME IN THE DAY

  5. #5
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: basic treeview

    I have to used two methods HitTest and SelectedItem, but I prefer HitTest to find the selected item:


    HitTest Method

    vb Code:
    1. Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    2. Dim tvwItem As MSComctlLib.Node
    3.  
    4.     Set tvwItem = TreeView1.HitTest(x, y)
    5.     If Not (tvwItem Is Nothing) Then Label1.Caption = tvwItem.Text
    6.  
    7. Set tvwItem = Nothing
    8. End Sub

    or SelectedItem Method

    vb Code:
    1. Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    2. Dim tvwItem As MSComctlLib.Node
    3.  
    4.     Set tvwItem = TreeView1.SelectedItem
    5.     If Not (tvwItem Is Nothing) Then Label1.Caption = tvwItem.Text
    6.  
    7. Set tvwItem = Nothing
    8. End Sub
    Last edited by Mark Gambo; Oct 8th, 2007 at 06:27 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: basic treeview

    IMO you should focus on how you store the data (and subsequent different retrieval criteria) before worrying too much of the interface; function over form. Are you already using a database (and using primary keys), or storing the info as XML (again with unique IDs)?

  7. #7
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: basic treeview

    Another trick you can use is simulating a ToolTip Text for each node. If you check you will see that the individual nodes don't have a ToolTipText property, when loading the nodes set it ToolTipText to what ever you want it to be in its Tag Property:

    vb Code:
    1. tvwSubItem.Tag = "ToolTip Text Here"

    and then use the following code:

    vb Code:
    1. Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    2. Dim tvwItem As MSComctlLib.Node
    3.  
    4.     Set tvwItem = TreeView1.HitTest(x, y)
    5.     If Not (tvwItem Is Nothing) Then TreeView1.ToolTipText = tvwItem.Tag
    6.  
    7. Set tvwItem = Nothing
    8. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  8. #8
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: basic treeview

    Quote Originally Posted by leinad31
    IMO you should focus on how you store the data (and subsequent different retrieval criteria) before worrying too much of the interface; function over form. . . .

    Most times you are correct, but I have designed applications where I had to design the front end first then the backend due to the customer's constant requests for new features and functions. Don't worry they paid handsomely for those changes
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  9. #9
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: basic treeview

    Here is the complete code with the ToolTextTips with Parent, Child, GrandChild and Great GrandChild nodes:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim tvwItem As MSComctlLib.Node, tvwSubItem As MSComctlLib.Node
    5. Dim tvwSubSubItem As MSComctlLib.Node
    6. Dim a As Long, b As Long, c As Long
    7.  
    8.     With TreeView1
    9.         .Appearance = cc3D
    10.         .Checkboxes = True
    11.         .LineStyle = tvwTreeLines
    12.        
    13.         With .Nodes
    14.             Set tvwItem = .Add(, , "Root")
    15.             tvwItem.Text = "Parent"
    16.             tvwItem.Tag = "Root"
    17.            
    18.             For a = 1 To 10
    19.                 Set tvwItem = .Add("Root", tvwChild, "Item " & a)
    20.                 tvwItem.Text = "Child " & a
    21.                 tvwItem.Bold = (a Mod 3)
    22.                 tvwItem.Checked = (a Mod 2)
    23.                 tvwItem.ForeColor = (25 * a)
    24.                 tvwItem.Tag = "ToolTip Text - " & tvwItem.Text
    25.                
    26.                 For b = 1 To 5
    27.                     Set tvwSubItem = .Add(tvwItem.Key, tvwChild, a & "SubItem " & b)
    28.                     tvwSubItem.Text = "GrandChild " & b
    29.                     tvwSubItem.Tag = "ToolTip Text - " & tvwSubItem.Text
    30.                    
    31.                     If b Mod 3 Then
    32.                         For c = 1 To 3
    33.                             Set tvwSubSubItem = .Add(tvwSubItem.Key, tvwChild, a & "SubSubItem " & b & "-" & c)
    34.                             tvwSubSubItem.Text = "Great GrandChild " & c
    35.                             tvwSubSubItem.Tag = "ToolTip Text - " & tvwSubSubItem.Text
    36.                         Next c
    37.                     End If
    38.                 Next b
    39.             Next a
    40.         End With
    41.    
    42.         For Each tvwItem In .Nodes
    43.             tvwItem.Expanded = (tvwItem.Key = "Root") ' Will expand all children root nodes
    44.         Next
    45.        
    46.         .Nodes.Item("Root").Selected = True ' Will ensure that root node is visible
    47.    
    48.     End With
    49.    
    50. Set tvwItem = Nothing
    51. Set tvwSubItem = Nothing
    52. Set tvwSubSubItem = Nothing
    53.  
    54. End Sub
    55.  
    56. Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    57. Dim tvwItem As MSComctlLib.Node
    58.  
    59.     Set tvwItem = TreeView1.HitTest(x, y)
    60.     If Not (tvwItem Is Nothing) Then TreeView1.ToolTipText = tvwItem.Tag
    61.  
    62. Set tvwItem = Nothing
    63. End Sub
    64.  
    65. Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    66. Dim tvwItem As MSComctlLib.Node
    67.  
    68.     Set tvwItem = TreeView1.SelectedItem
    69.     If Not (tvwItem Is Nothing) Then Label1.Caption = tvwItem.Text
    70.  
    71. Set tvwItem = Nothing
    72. End Sub
    Last edited by Mark Gambo; Oct 8th, 2007 at 06:48 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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