+ Reply to Thread
Results 1 to 2 of 2

Moving Treeview Node using button Up and Down

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    11

    Cool Moving Treeview Node using button Up and Down

    I have been looking for a solution that could reorder or move nodes up and down using a button as trigger. But I have no luck finding it. Does anyone have any idea about this? Or this is just a dead end?

  2. #2
    Registered User
    Join Date
    08-19-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Moving Treeview Node using button Up and Down

    I just figured this on the other forum. Here's what I came up with.

    Private Sub cmdDown_Click()
    MoveDown
    End Sub
    
    Private Sub cmdUP_Click()
    MoveUp
    End Sub
    
    Function MoveUp()
    Dim nodX As Node, Node As Node
    Dim i As Integer, nodPos As Long
    Dim SkipNode As Boolean
    Dim myMessage As VbMsgBoxResult
    Dim tvSelObj As MSComctlLib.TreeView
    Dim tempNodeText As String
    
    On Error GoTo ErrHandler
    
    Set tvSelObj = frmDataVal.tvSelectedDVs
    If tvSelObj.SelectedItem Is Nothing Then Exit Function
    
    nodPos = 1
    For i = 0 To tvSelObj.Nodes.Count
        SkipNode = False
        If i = tvSelObj.SelectedItem.Index Then
            If i = nodPos Then SkipNode = True
            nodPos = nodPos + 1
            If SkipNode = False Then
                tempNodeText = tvSelObj.SelectedItem.Previous.Text
                tvSelObj.Nodes(tvSelObj.SelectedItem.Previous.Index).Text = tvSelObj.Nodes(tvSelObj.SelectedItem.Index).Text
                tvSelObj.Nodes(tvSelObj.SelectedItem.Index).Text = tempNodeText
                tvSelObj.SelectedItem.Selected = False
                tvSelObj.SelectedItem.Previous.Selected = True
            End If
        End If
    Next
    
    ErrHandler:
    
    End Function
    Function MoveDown()
    Dim nodX As Node, Node As Node
    Dim i As Integer, nodPos As Long, parentCount As Integer
    Dim SkipNode As Boolean
    Dim myMessage As VbMsgBoxResult
    Dim tvSelObj As MSComctlLib.TreeView
    Dim tempNodeText As String
    
    On Error GoTo ErrHandler
    
    Set tvSelObj = frmDataVal.tvSelectedDVs
    If tvSelObj.SelectedItem Is Nothing Then Exit Function
    
    nodPos = 1
    For i = tvSelObj.Nodes.Count To 0 Step -1
        SkipNode = False
        If i = tvSelObj.SelectedItem.Index Then
            If i = nodPos Then SkipNode = True
            nodPos = nodPos + 1
            If SkipNode = False Then
                tempNodeText = tvSelObj.SelectedItem.Next.Text
                tvSelObj.Nodes(tvSelObj.SelectedItem.Next.Index).Text = tvSelObj.Nodes(tvSelObj.SelectedItem.Index).Text
                tvSelObj.Nodes(tvSelObj.SelectedItem.Index).Text = tempNodeText
                tvSelObj.SelectedItem.Selected = False
                tvSelObj.SelectedItem.Next.Selected = True
            End If
        End If
    Next
    
    ErrHandler:
    End Function
    The only problem with this is that if you try to move the parent node, the children won't follow.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Moving Treeview Node using button Up and Down
    By vmvelasco in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 03-28-2019, 01:25 PM
  2. How to add GrandChild node in TreeView??
    By sarat528 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2018, 05:22 AM
  3. Get Node Key from selected Node in TreeView
    By mile990 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2016, 04:55 PM
  4. Counting the Parents of a Treeview Node?
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-14-2015, 08:39 PM
  5. [SOLVED] Get Treeview selected node into a string.
    By abhay_547 in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 11-06-2010, 02:15 PM
  6. treeview node tag hold array?
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-26-2005, 08:05 PM
  7. delete a node in treeview
    By mark in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-15-2005, 12:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1