Solved

Edit a treeview-node and then sort the treeview

Posted on 2011-03-23
2
987 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:peterkiers
2 Comments
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 500 total points
ID: 35200368
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
 
LVL 1

Author Comment

by:peterkiers
ID: 35202673
Hi, first its VB and second its not the solution.

Peter
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now