Probably a simple TreeView question about adding a subnode in code ... This is the first time I've used it...

   I want to build a TreeView ( Add nodes)  in code when a Form Opens.  How do I set the selected node = a previously defined node in code.

  Private Sub Create_TreeView()
        ' Adds new node as a child node of the currently selected node.
        Dim newRoot As New TreeNode
        Dim newMain As New TreeNode

        newRoot.Text = "Root"
        newMain.Text = "Main Categories"


        'How do I set Selected Node to  "Main Categories"
        ' so that the the next node added is a subNode of "Main Categories"?

        Dim newMainSub01 As New TreeNode
        newMainSub01.Text = "Category A"


      The Output should look similar to this:

       Main Categories
       --Category A

    End Sub
Who is Participating?
You don't need to "select" the node in the TreeView. Just add the subnode directly to it:


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.