Solved

Edit a treeview-node and then sort the treeview

Posted on 2011-03-23
2
998 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

20 Experts available now in Live!

Get 1:1 Help Now