Solved

Edit a treeview-node and then sort the treeview

Posted on 2011-03-23
2
1,027 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 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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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