REORDER NODES IN A TREEVIEW
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: REORDER NODES IN A TREEVIEW

  1. #1
    Join Date
    Mar 2002
    Posts
    14

    REORDER NODES IN A TREEVIEW

    Hello All,

    Does anybody know how to move a node up or down in a treeview ctrl, in a same level.
    Say in other words, moving a node before its upper brother or after its lower brother

    changing something like node.previous or node.next


  2. #2
    Join Date
    Jun 2001
    Location
    Ireland
    Posts
    56

    Re: REORDER NODES IN A TREEVIEW

    Is this in Runtime or design time.?
    If Runtime how do you want call this. Just let me know and I'll tell you all I can.

    Andy

    Don't forget to rate!!


    Andrew Lennon (Berlitz)
    Automation Dev Engineer

  3. #3
    Join Date
    Mar 2002
    Posts
    14

    Re: REORDER NODES IN A TREEVIEW

    This is at runtime. I want for example :

    1 - user select a node
    2 - hit a button UP or DOWN
    ==> The node goes up or down until it reach the first or the last position of its current level



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

    Re: REORDER NODES IN A TREEVIEW

    I have an example of just this thing but is to large to post here. Send me a private message with your email address and I will send you a Zip file. Please refer to the Treeview node moveing up or down so I can remember what your note is talking about.

    Please rate it if it answers the question
    or is useful.
    '
    John G

  5. #5
    Join Date
    Jun 2001
    Location
    Ireland
    Posts
    56

    Re: REORDER NODES IN A TREEVIEW

    Sorry this took so long! It's a little scrappy but it works. Remember to put two buttons on the form one fro UP and one for DOWN. This code snippet is for the UP button so all you need to do is basically reverse the process.


    private Sub UP_Click()
    Dim STR as string
    Dim x as Node
    Dim i as Integer
    Dim newstr1 as string
    Dim newstr2 as string
    Dim keya as string
    Dim keyb as string

    'tvw in this case is the TreeView

    'get selected node values
    i = N.Index - 1
    newstr1 = N.Text
    keya = N.Key

    set x = me.tvw.Nodes(i)

    newstr2 = x.Text

    'reset the node key values
    keya = N.Key
    keyb = x.Key

    N.Key = "ww"
    x.Key = "xx"

    'Reset the new node values
    x.Text = newstr1
    x.Key = keya

    N.Text = newstr2
    N.Key = keyb


    End Sub





    Andy

    Don't forget to Rate!!!

    Andrew Lennon (Berlitz)
    Automation Dev Engineer

  6. #6
    Join Date
    Mar 2002
    Posts
    14

    Re: REORDER NODES IN A TREEVIEW

    Thank you for your interset, Andy, but the code you send me is not exactly what I need : you just switch the key and text values, but the job is not done on the children of the node you move. Your code does if you move UP the NODE_C:

    NODE_A.............................NODE_A
    |............................................
    --NODE_B...........................--NODE_C
    --NODE_C...........................--NODE_B
    --|..........................................--|
    -----NODE_D........................-----NODE_D
    -----NODE_E........................-----NODE_E

    What I want is :

    NODE_A.............................NODE_A
    |............................................
    --NODE_B...........................--NODE_B
    --NODE_C...........................--
    --|..........................................-----NODE_D
    -----NODE_D........................-----NODE_E
    -----NODE_E........................--NODE_A

    Perhaps, you know how to do that ?

    Thanks.

    Christian


  7. #7
    Join Date
    Jun 2001
    Location
    Ireland
    Posts
    56

    Re: REORDER NODES IN A TREEVIEW

    Now you've hit a problem. There is no way to move up or down Nodes with children using Command Buttons. The only I know how move such quantity of nodes is Drag and Drop. If you need help with this just let me know.


    Don't forget to rate!!!

    Andrew Lennon (Berlitz)
    Automation Dev Engineer

  8. #8
    Join Date
    Dec 2012
    Posts
    1

    Re: REORDER NODES IN A TREEVIEW

    After an extensive search with no luck I have found a way to easily move up and down using a command button in a tree view. It's not pretty but it works well.

    My example only goes 5 levels deep, you could continue on with more or remove if you don't need as many.


    Code:
    Private Sub btnUp_Click(sender As System.Object, e As System.EventArgs) Handles btnUp.Click
            Dim myNode As TreeNode = Nothing
            Dim i As Integer = 0
            Dim x As Integer = 0
            Dim L1 As String = String.Empty
            Dim L2 As String = String.Empty
            Dim L3 As String = String.Empty
            Dim L4 As String = String.Empty
            Dim L5 As String = String.Empty
    
    
    
      If tvw1.SelectedNode.PrevNode Is Nothing Then
                MessageBox.Show("Top Of Node - Unable To Move Up", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                x = tvw1.SelectedNode.Level
                i = tvw1.SelectedNode.PrevNode.Index
                Select Case x
                    Case 0
                        myNode = tvw1.SelectedNode
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes.Insert(i, myNode)
                    Case 1
                        myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L1).Nodes.Insert(i, myNode)
                    Case 2
                       myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case 3
                       myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        L3 = myNode.Parent.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L3).Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case 4
                       myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        L3 = myNode.Parent.Parent.Parent.Name
                        L4 = myNode.Parent.Parent.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L4).Nodes(L3).Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case 5
                       myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        L3 = myNode.Parent.Parent.Parent.Name
                        L4 = myNode.Parent.Parent.Parent.Parent.Name
                        L5 = myNode.Parent.Parent.Parent.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L5).Nodes(L4).Nodes(L3).Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case Else
                       MessageBox.Show("Can Not Move Up Too Deep In Treeview", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Select
            End If
        End Sub
    
    
    
    
        Private Sub btnDn_Click(sender As System.Object, e As System.EventArgs) Handles btnDn.Click
            Dim myNode As TreeNode = Nothing
            Dim i As Integer = 0
            Dim x As Integer = 0
            Dim L1 As String = String.Empty
            Dim L2 As String = String.Empty
            Dim L3 As String = String.Empty
            Dim L4 As String = String.Empty
            Dim L5 As String = String.Empty
    
    
    
             If tvw1.SelectedNode.NextNode Is Nothing Then
                 MessageBox.Show("Unable To Move Down", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            Else
                 x = tvw1.SelectedNode.Level
                i = tvw1.SelectedNode.NextNode.Index
                Select Case x
                     Case 0
                        myNode = tvw1.SelectedNode
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes.Insert(i, myNode)
                    Case 1
                        myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L1).Nodes.Insert(i, myNode)
                    Case 2
                        myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case 3
                         myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        L3 = myNode.Parent.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L3).Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case 4
                        myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        L3 = myNode.Parent.Parent.Parent.Name
                        L4 = myNode.Parent.Parent.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L4).Nodes(L3).Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case 5
                        myNode = tvw1.SelectedNode
                        L1 = myNode.Parent.Name
                        L2 = myNode.Parent.Parent.Name
                        L3 = myNode.Parent.Parent.Parent.Name
                        L4 = myNode.Parent.Parent.Parent.Parent.Name
                        L5 = myNode.Parent.Parent.Parent.Parent.Parent.Name
                        tvw1.Nodes.Remove(myNode)
                        tvw1.Nodes(L5).Nodes(L4).Nodes(L3).Nodes(L2).Nodes(L1).Nodes.Insert(i, myNode)
                    Case Else
                        MessageBox.Show("Can Not Move Down Too Deep In Treeview", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Select
            End If
        End Sub
    *** MOD EDIT **** Please use CODE tags.. It keeps the code formatting, and also frames it for easy identification, also it's allot easier to use than the Indent tags that you spread across the post...
    Last edited by GremlinSA; December 13th, 2012 at 01:18 AM. Reason: Please use code tags.....

  9. #9
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: REORDER NODES IN A TREEVIEW

    Do it the same way you'd re-order a stack of blocks. You can't add or remove from the middle. Usually easiest is to just create a new version.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center