schworak
asked on
Moving a TreView node (resequencing)
I have a treeview with several items in it such as this...
Folder 1
Item 1a
Item 1b
Item 1c
Item 1d
Folder 2
Item 2a
Item 2b
Item 2c
Item 2d
Folder 3
Item 3a
Item 3b
Item 3c
Item 3d
Without using DRAG-DROP I want to resequence some items. I just want to take Item 3c and move it to be before item 3a.
I also want to be able to move items from one folder to another in the same way such as moving item 2c to be right after item 1c.
Now I can simply create a new node item in the new location and delete the old item. I am wondering if there is another way to resequence items in the tree without moving them. I can change the parent but that won't get the item in the right location (at least not from what I have read and tested).
So if you know the trick, let me know.
I need an answer really fast, otherwise I will get stuck doing the "Creat New then Delete" trick which I think is very ugly.
Folder 1
Item 1a
Item 1b
Item 1c
Item 1d
Folder 2
Item 2a
Item 2b
Item 2c
Item 2d
Folder 3
Item 3a
Item 3b
Item 3c
Item 3d
Without using DRAG-DROP I want to resequence some items. I just want to take Item 3c and move it to be before item 3a.
I also want to be able to move items from one folder to another in the same way such as moving item 2c to be right after item 1c.
Now I can simply create a new node item in the new location and delete the old item. I am wondering if there is another way to resequence items in the tree without moving them. I can change the parent but that won't get the item in the right location (at least not from what I have read and tested).
So if you know the trick, let me know.
I need an answer really fast, otherwise I will get stuck doing the "Creat New then Delete" trick which I think is very ugly.
If this is a big tree and you are resequencing a lot of nodes such as a quicksort or bubblesort, it will NOT be fast. A faster alternative is to extract everything from the tree (into an array perhaps), resequence the array, blow away the tree and rebuild from your resequenced array.
ASKER
No, it is usually only a couple nodes that need to move in a large tree. So speed is not a consern. It is more of a pain because I have to take the existing node and change its key to a unique value, then create the new node with all the same attributes including the key. Then go back and delete the old node.
I was just hoping I could change its index or previous value. I can change the parent but that switches it to a new folder.
I will leave this open in the hopes that there is a solution.
I was just hoping I could change its index or previous value. I can change the parent but that switches it to a new folder.
I will leave this open in the hopes that there is a solution.
ASKER
Here is the code that I came up with for anyone that also needs this tool. This is very fast! I am mainly using it to drag and drop nodes but I am also using it to sort segments of the tree as I need.
Public Function MoveNode(ByRef Tree As MSComctlLib.TreeView, ByRef SourceNode As MSComctlLib.Node, ByRef TargetNode As MSComctlLib.Node, ByVal InsertMode As Long) As MSComctlLib.Node
'
' Move the source (and any child nodes) to the target using the
' specified insert mode which should be one of the following...
'
' tvwChild Make a child of the target
' tvwPrevious Place before the target
' tvwNext Place after the target
'
' Returns a pointer to the new node in the new location
'
'
Dim n As MSComctlLib.Node
Dim ch1 As MSComctlLib.Node
Dim ch2 As MSComctlLib.Node
Dim k As String
k = SourceNode.Key
SourceNode.Key = ""
Set n = Tree.Nodes.Add(TargetNode, InsertMode, k, SourceNode.Text, SourceNode.Image, SourceNode.SelectedImage)
n.Expanded = SourceNode.Expanded
n.ExpandedImage = SourceNode.ExpandedImage
n.BackColor = SourceNode.BackColor
n.Bold = SourceNode.Bold
n.Checked = SourceNode.Checked
n.ForeColor = SourceNode.ForeColor
n.Sorted = SourceNode.Sorted
n.EnsureVisible
Set ch1 = SourceNode.Child
If ch1 Is Nothing Then
' Do nothing
Else
Set ch1 = ch1.LastSibling
Do Until ch1 Is Nothing
Set ch2 = ch1.Previous
Set ch1.Parent = n
Set ch1 = ch2
Loop
End If
Tree.Nodes.Remove SourceNode.Index
Set Tree.DropHighlight = Nothing
n.Selected = True
Set MoveNode = n
End Function
I then call this function like this...
set NewNodeLocation = MoveNode(MyTree,DragNode,D ropNode,tv wNext)
Public Function MoveNode(ByRef Tree As MSComctlLib.TreeView, ByRef SourceNode As MSComctlLib.Node, ByRef TargetNode As MSComctlLib.Node, ByVal InsertMode As Long) As MSComctlLib.Node
'
' Move the source (and any child nodes) to the target using the
' specified insert mode which should be one of the following...
'
' tvwChild Make a child of the target
' tvwPrevious Place before the target
' tvwNext Place after the target
'
' Returns a pointer to the new node in the new location
'
'
Dim n As MSComctlLib.Node
Dim ch1 As MSComctlLib.Node
Dim ch2 As MSComctlLib.Node
Dim k As String
k = SourceNode.Key
SourceNode.Key = ""
Set n = Tree.Nodes.Add(TargetNode,
n.Expanded = SourceNode.Expanded
n.ExpandedImage = SourceNode.ExpandedImage
n.BackColor = SourceNode.BackColor
n.Bold = SourceNode.Bold
n.Checked = SourceNode.Checked
n.ForeColor = SourceNode.ForeColor
n.Sorted = SourceNode.Sorted
n.EnsureVisible
Set ch1 = SourceNode.Child
If ch1 Is Nothing Then
' Do nothing
Else
Set ch1 = ch1.LastSibling
Do Until ch1 Is Nothing
Set ch2 = ch1.Previous
Set ch1.Parent = n
Set ch1 = ch2
Loop
End If
Tree.Nodes.Remove SourceNode.Index
Set Tree.DropHighlight = Nothing
n.Selected = True
Set MoveNode = n
End Function
I then call this function like this...
set NewNodeLocation = MoveNode(MyTree,DragNode,D
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Darko