locked
How to Set focus programmatically on Treeview node RRS feed

  • Question

  • see the code which i am using to move up and down selected node but after move up and down some time i see selection on selected node goes out. so please tell me what to change in my code as a result selection should not go from selected cell after movement up/down ? here is code

    /// <summary>
            /// Move Selected Node Up in TreeNode Collection
            /// </summary>
            /// <param name="node"></param>
            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);
                        node.Text = node.Text;
                    }
                }
                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);
                        node.Text = node.Text;
                    }
                }
            }
    
            /// <summary>
            /// Move Selected Node Down in TreeNode Collection
            /// </summary>
            /// <param name="node"></param>
            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);
                        node.Text = node.Text;
                    }
                }
                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);
                        node.Text = node.Text;
                    }
                }
            }

    • Moved by CoolDadTx Tuesday, September 22, 2020 6:18 PM Winforms related
    Tuesday, September 22, 2020 2:17 PM

All replies

  • This forum is for C#-specific questions only. Please post questions related to Windows Forms in the new Q&A forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, September 22, 2020 6:18 PM
  • Sir in that forum there is no section for c# or winform. so how to post this question there ?

    under which category this question should be posted? please guide. thanks

    Wednesday, September 23, 2020 7:26 AM