Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Edit a treeview-node and then sort the treeview

Posted on 2011-03-23
2
Medium Priority
?
1,055 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

609 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