Trreview problem with moving a node

Started by CFred, July 25, 2025, 12:19:19 PM

Previous topic - Next topic

CFred

The attached program attempts to move a leaf node in a tree when it is dragged to a different position.

The program almost works except if I drag a leaf node to another position and then I drag another leaf mode to make it a child node of the previously dragged leaf node the second dragged node disappears.

I've spent a long time trying to fix this issue without success. Can anybody help me to solve this issue?

CFred

At last I managed to find a bug in my code at line 560 in the previously uploaded version of the code in the function TREE_MoveSingleBranch().

I changed the line:

tvItem.mask = TVIF_HANDLE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM | TVIF_STATE | TVIF_CHILDREN;
to:

tvItem.mask = TVIF_HANDLE | TVIF_TEXT;
I am uploading the working version in case anybody finds it useful (there is very little information on the internet that I could find for dragging tree nodes, particularly as Code Project is no longer available). The code is heavily commented to explain how the it works. I have not tidied up the code so I leave this for anybody who wants to use it.

John Z

Thanks CFred!

Very nice.  Grabbed it for learning and future reference. 
I've used TreeView quite a bit, but never needed to drag and drop a leaf,

John Z

CFred

I have updated the demonstration code in previous posts to tidy it up. I have also included the use of two different icons for parent and child nodes and a "No entry" cursor when dragging a node to itself.

The icons taken from The Silk icon pack (was available on the famfamfam website - no longer functioning), whose author is Mark James.

The code needs more work so that subtrees can be moved. I will post a version for moving subtrees once I have developed it fully. At the moment this is work in progress.




CFred

I have implemented a treeview control with full drop and drag functionality at https://forum.pellesc.de/index.php?topic=11646.0