Einhugur TreeView Drag node

I need to drag nodes in tree view to re-order nodes.

I try


Function MouseDown(x as Integer,y as Integer,isRightButton as Boolean) As Boolean
    // We return true here to Enable the BeginDragNode event 
   System.DebugLog("MouseDown")
    return true
End Function
Sub BeginDragNode(node as TreeViewNode,nodeIndex as Integer,drag as DragItem)
    // ------------ 
    // Here you might want to put some data on the drag item
    // ------------
    System.DebugLog("Begin Drag")
    drag.Drag()
End Sub

Mousedown event is fired as excepted but not Begin Drag.

Somebody have any feedback about drag and drop node in a TreeView ?

Are you sure you do not get the later event ?

I was testing and thought at first I was not getting it. Then as I realized Xojo bug then I got things working:

Sub BeginDragNode(node as TreeViewNode, nodeIndex as Integer, drag as DragItem) Handles BeginDragNode

drag.Picture = FolderIconNormal // The fact I need this for drag to show seems to be bug in Xojo DragItem
drag.Drag()
End Sub

As in I had to set Picture there on thee Drag even if it does not seem to use the Picture.

I dont know for sure if thats Xojo bug or intended to work like that

Well not Xojo bug, its just that some data needs to be put on the Drag, Picture or Text minimum it seems.

Its something I should make note of in the documentation.

1 Like

If I change my code with:

Sub BeginDragNode(node as TreeViewNode, nodeIndex as Integer, drag as DragItem) Handles BeginDragNode
  System.DebugLog("Begin Drag")
  drag.Text = "Test"
  drag.Drag()
End Sub

Both MouseDown and Begin Drag events are fired.

My goal is to Drag a node (not a text, picture or folder item) to re-order my tree view. @Björn_Eiríksson do you have any idea?

Its a little tricky but possible, I have done it somewhat in our Einhugur Notes app

I don’t know if you can harvest something from this code its a bit centric to our app. It gives you maybe high level idea what you need to check for like you do not want parent or ancestor to be able to drop on child from same family branch.

Sub BeginDragNode(node as TreeViewNode, nodeIndex as Integer, drag as DragItem) Handles BeginDragNode
  if tbSearch.Text = "" and m_SelectedTab <> 2 then
    m_NodeDragged = node
    drag.RawData("TVND") = "Node"
    drag.Drag()
  end if
  
End Sub

Sub DropObject(dropOn as TreeViewNode, obj as DragItem) Handles DropObject
  Dim nodeData as NodeItem
  Dim dropNodeData as NodeItem
  Dim treeSteps(-1) as Integer
  Dim node as TreeViewNode
  
  if m_NodeDragged = nil then
    MsgBox AppLocalization.NodeNotFromThisDocument
    return
  end if
  
  if dropOn = m_NodeDragged then
    return
  end if
  
  nodeData = NodeItem(m_NodeDragged.ItemData)
  
  if dropOn = nil then
    if tvList.GetNodeParent(m_NodeDragged) <> nil then
      m_Document.ReparentNode(nodeData.ID, 0)
    end if
  else
    dropNodeData = NodeItem(dropOn.ItemData)
    
    // Check if we are dropping on child of our self
    if m_Document.IsChildOf(dropNodeData.ID, nodeData.ID) then
      MsgBox AppLocalization.NodesDescendantCannotBeNodesParent
      return
    end if
    
    m_Document.ReparentNode(nodeData.ID, dropNodeData.ID)
    
    
    
    treeSteps.Append(NodeItem(dropOn.ItemData).ID)
    
    node = tvList.GetNodeParent(dropOn)
    
    while node <> nil and  node.Depth <> 0
      treeSteps.Append(NodeItem(node.ItemData).ID)
      
      node = tvList.GetNodeParent(node)
    wend
  end if
  
  Filter()
  
  if treeSteps.Ubound >= 0 then
    for i as Integer = 0 to tvList.RootNodeCount - 1
      if NodeItem(tvList.RootNodes(i).ItemData).ID = treeSteps(treeSteps.Ubound) then
        node = tvList.RootNodes(i)
        node.SetExpanded(true,true)
        treeSteps.Remove(treeSteps.Ubound)
        exit for
      end if
    next
    
    While treeSteps.Ubound >= 0 and node <> nil
      for i as Integer = 0 to node.NodeCount - 1
        if NodeItem(node.Node(i).ItemData).ID = treeSteps(treeSteps.Ubound) then
          node = node.Node(i)
          node.SetExpanded(true,true)
        end if
      next
      treeSteps.Remove(treeSteps.Ubound)
    wend
  end if
End Sub

Sub Open() Handles Open

  me.AcceptRawDataDrop("TVND")
End Sub
1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.