Edit a treeview-node and then sort the treeview

Hi,

I have found a question and solution on the internet:

Question:
After a node's label is edited in the tree I try to resort the nodes to place the updated item in the right position. I do this by calling .Sort in AfterLabelEdit event handler which causes an infinite loop.
How can I resort the nodes in a treeview after a label has been changed?

Answer:

Use BeginInvoke:

delegate void sort();

    private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
    {
        treeView1.BeginInvoke(new sort(treeView1.Sort));
    }

I have the same problem but I don't know how to implement it in my
code. I have put the code for editing a treeview node in the code-section.

Who can help me?

Peter
private void MainForm_Load(object sender, EventArgs e)
        {
        tvSampleMenu.MenuItems.Add("Edit", new EventHandler(tvSampleRightClickEdit));
         ....
        }
                //*----------------------------------------------------------*//
        private void tvSampleRightClickEdit(object sender, System.EventArgs e)
        {
                TreeNode node = tvNotes.SelectedNode;
                if (node == null) { return; }
                if (node == tvNotes.Nodes[0]) { return; }
                node.TreeView.LabelEdit = true;
                node.BeginEdit();
            }
          //*----------------------------------------------------------*//
        private void tvNotes_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
         {
             if (e.Label.Trim().Length < 1) { e.CancelEdit = true; }  
             EditNode(tvNotes.SelectedNode,e.Label);
             tvNotes.SelectedNode.EndEdit(false);
             tvNotes.LabelEdit = false;
         }
         //*----------------------------------------------------------*//
         private void EditNode(TreeNode node, string newText)
        {
            DataRow row = null;
            {
                if (node == null) { return; }
                if (node == tvNotes.Nodes[0]) { return; }
                row = (DataRow)node.Tag;
                if (row == null)
                {
                    return;
                }
                row["Description"] = newText;
            }
            //*----------------------------------------------------------*//

Open in new window

LVL 1
peterkiersAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
SAMIR BHOGAYTAConnect With a Mentor Freelancer and IT ConsultantCommented:
Hi, use this code



   Private Sub ManningFiltersTreeView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ManningFiltersTreeView.DragDrop
     
            'Check that there is a TreeNode being dragged
            If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
                  True) = False Then Exit Sub
     
            'Get the TreeView raising the event (incase multiple on form)
            Dim selectedTreeview As TreeView = CType(sender, TreeView)
     
           'Get the TreeNode being dragged
           Dim dropNode As TreeNode = _
                 CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
                 TreeNode)
   
           'The target node should be selected from the DragOver event
           Dim targetNode As TreeNode = selectedTreeview.SelectedNode
   
           'Remove the drop node from its current location
           dropNode.Remove()
   
           'If there is no targetNode add dropNode to the bottom of
           'the TreeView root nodes, otherwise add it to the end of
           'the dropNode child nodes
           If targetNode Is Nothing Then
               selectedTreeview.Nodes.Add(dropNode)
           Else
               targetNode.Nodes.Add(dropNode)
           End If
   
           'Ensure the newly created node is visible to
           'the user and select it
           dropNode.EnsureVisible()
           selectedTreeview.SelectedNode = dropNode
   
       End Sub
   
       Private Sub ManningFiltersTreeView_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ManningFiltersTreeView.DragEnter
   
           'See if there is a TreeNode being dragged
           If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
               True) Then
               'TreeNode found allow move effect
               e.Effect = DragDropEffects.Move
           Else
               'No TreeNode found, prevent move
               e.Effect = DragDropEffects.None
           End If
   
       End Sub
   
       Private Sub ManningFiltersTreeView_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ManningFiltersTreeView.DragOver
   
           'Check that there is a TreeNode being dragged
           If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
                  True) = False Then Exit Sub
   
           'Get the TreeView raising the event (incase multiple on form)
           Dim selectedTreeview As TreeView = CType(sender, TreeView)
   
           'As the mouse moves over nodes, provide feedback to
           'the user by highlighting the node that is the
           'current drop target
           Dim pt As Point = _
               CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
           Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)
   
           'See if the targetNode is currently selected,
           'if so no need to validate again
           If Not (selectedTreeview.SelectedNode Is targetNode) Then
               'Select the    node currently under the cursor
               selectedTreeview.SelectedNode = targetNode
   
               'Check that the selected node is not the dropNode and
               'also that it is not a child of the dropNode and
               'therefore an invalid target
               Dim dropNode As TreeNode = _
                   CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
                   TreeNode)
   
               Do Until targetNode Is Nothing
                   If targetNode Is dropNode Then
                       e.Effect = DragDropEffects.None
                       Exit Sub
                   End If
                   targetNode = targetNode.Parent
               Loop
           End If
   
           'Currently selected node is a suitable target
           e.Effect = DragDropEffects.Move
           'End If
   
       End Sub
   
       Private Sub ManningFiltersTreeView_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ManningFiltersTreeView.ItemDrag
   
           'Set the drag node and initiate the DragDrop
           DoDragDrop(e.Item, DragDropEffects.Move)
   
      End Sub

0
 
peterkiersAuthor Commented:
Hi, first its VB and second its not the solution.

Peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.