Treeview Node Move Up/Down and selection on node gets lost

Sudip Bhatt 2,276 Reputation points
2020-12-09T08:41:13.17+00:00

This way i am moving treeview node but selection from selected node gets lost after move. how could i persist selection on selected node after being moved up/down.

public static class Extensions
{
    public static void MoveUp(this TreeNode node)
    {
        TreeNode parent = node.Parent;
        TreeView view = node.TreeView;
        if (parent != null)
        {
            int index = parent.Nodes.IndexOf(node);
            if (index > 0)
            {
                parent.Nodes.RemoveAt(index);
                parent.Nodes.Insert(index - 1, node);
            }
        }
        else if (node.TreeView.Nodes.Contains(node)) //root node
        {
            int index = view.Nodes.IndexOf(node);
            if (index > 0)
            {
                view.Nodes.RemoveAt(index);
                view.Nodes.Insert(index - 1, node);
            }
        }
    }

    public static void MoveDown(this TreeNode node)
    {
        TreeNode parent = node.Parent;
        TreeView view = node.TreeView;
        if (parent != null)
        {
            int index = parent.Nodes.IndexOf(node);
            if (index < parent.Nodes.Count -1)
            {
                parent.Nodes.RemoveAt(index);
                parent.Nodes.Insert(index + 1, node);
            }
        }
        else if (view != null && view.Nodes.Contains(node)) //root node
        {
            int index = view.Nodes.IndexOf(node);
            if (index < view.Nodes.Count - 1)
            {
                view.Nodes.RemoveAt(index);
                view.Nodes.Insert(index + 1, node);
            }
        }
    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2020-12-10T09:20:50.577+00:00

    Hi SudipBhatt-9737,
    You need to add this line(node.TreeView.SelectedNode = node) to restore the originally selected node as selected.
    So you need to change you code to the below:

    parent.Nodes.Insert(index - 1, node);  
    node.TreeView.SelectedNode = node;  
      
    view.Nodes.Insert(index - 1, node);  
    node.TreeView.SelectedNode = node;  
      
    parent.Nodes.Insert(index + 1, node);  
    node.TreeView.SelectedNode = node;  
      
    view.Nodes.Insert(index + 1, node);  
    node.TreeView.SelectedNode = node;  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. harborsiem 1 Reputation point
    2020-12-15T18:09:15.287+00:00

    @Sudip Bhatt
    This added extension class for TreeNode is working well in my application RibbonTools (WindowsRibbon)

    48482-extension.txt

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.