Drag treeview named TreeView1, textbox named TextBox1,
button named Button1.

I m adding nodes programmatically. Select the node,Enter text in TextBox1 & then click on Button1 to add the node.

Mine code is doing this, I want that when the user add nodes it automatically sorts.

Suppose user selects All(node) that is added on form_load,enter 1 in textbox1, click on buuton1,node added.

Second time again user selects All,enter 3 in textbox1,click on button1, node added.

Now the user enters 2 in textbox,click on button,node added. I want that nodes are automatically sorted after adding.

I want


Currently i m getting output

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tnNew As New TreeNode
        Dim tnSelected As TreeNode

        If Trim(TextBox1.Text) = "" Then
            MsgBox("Enter text in Textbox")
            Exit Sub
        End If
        tnSelected = TreeView1.SelectedNode
        If Not tnSelected Is Nothing Then
            tnNew = tnSelected.Nodes.Add(TextBox1.Text)
        End If

        TextBox1.Text = ""
    End Sub
End Class

If you're only going to have 1 root node then I would go with a TreeView1.Sort() before TreeView1.ExpandAll().

If you have more than 1 root node, then the root nodes will be sorted as well so you may have to throw them into a list and sort the list then change their orders once the child nodes have been sorted.

Hope that helps!


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.