Results 1 to 7 of 7

Thread: [RESOLVED] Treeview selecting the added node

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Resolved [RESOLVED] Treeview selecting the added node

    I am bootstrapping myself up on using the Treeview control in VB. I have a quick question, after adding a child node using the following :

    Code:
    TreeView_Dir_List.SelectedNode.Nodes.Add(OpenFileDialog.SafeFileNames(0)) ' Add the new item
    How can I then make it (the newly added node) the selected node? I know I can use the selectednode property to 'set' it, but how do I reference it?

    Rick

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,901

    Re: Treeview selecting the added node

    // Sorry, I posted a VB6 solution, didn't see the topic is in VB.Net //

    2 methods:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      With TreeView1
        .Nodes.Clear
        .HideSelection = False
      End With
    End Sub
    
    Private Sub Command1_Click()
      ' Method 1
      Set TreeView1.SelectedItem = TreeView1.Nodes.Add(, , , "Another node")
    End Sub
    
    Private Sub Command2_Click()
      ' Method 2
      TreeView1.Nodes.Add(, , , "Another node").Selected = True
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Re: Treeview selecting the added node

    I do not see a selected property, there is a IsSelected but it is read only?

    Code:
    TreeView_Dir_List.SelectedNode.Nodes.Add(OpenFileDialog.SafeFileNames(0)).IsSelected
    I guess I am just in essence trying to find a way to reference the added node in order to set various properties. I could loop through the collection and test the name but I really thought there would be a more eloquent solution.

    Rick

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,901

    Re: Treeview selecting the added node

    Shouldn't it be something like this:
    Code:
    TreeView_Dir_List.SelectedNode = TreeView_Dir_List.SelectedNode.Nodes.Add(OpenFileDialog.SafeFileNames(0))

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Re: Treeview selecting the added node

    Yep, that worked...never really thought about it that 'way around'.... I had just assumed I could get a reference of some sort to the most recently added item..... Thanks a bunch!

    Rick

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,901

    Re: Treeview selecting the added node

    The .Add method returns the freshly added node, so you do get the reference.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Re: Treeview selecting the added node

    Yep, that was exactly what I was looking for....thank you!

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