Search code examples
c#winformsdevexpresstreelist

Move Node Up and Down In TreeLIist


In my project, I'm trying to create a treelist usercontrol to move node up and down with the help of buttons(up and down) and make focused node always visible in treelist

C#

private void button1_Click(object sender, EventArgs e)
        {

            int LastNodeIndex = treeList1.GetNodeIndex(treeList1.Nodes.LastNode);
            int targetNodeIndex = treeList1.GetNodeIndex(treeList1.FocusedNode.PrevNode);

            if(targetNodeIndex==-1)
            {
                treeList1.SetNodeIndex(treeList1.FocusedNode, LastNodeIndex);
                treeList1.MakeNodeVisible(treeList1.FocusedNode);
            }
            else
            {
                int nodeIndex = treeList1.GetNodeIndex(treeList1.FocusedNode);
                treeList1.SetNodeIndex(treeList1.FocusedNode, targetNodeIndex);
                treeList1.MakeNodeVisible(treeList1.FocusedNode);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int targetNodeIndex = treeList1.GetNodeIndex(treeList1.FocusedNode.NextNode);
            int nodeIndex = treeList1.GetNodeIndex(treeList1.FocusedNode);
            treeList1.SetNodeIndex(treeList1.FocusedNode, targetNodeIndex);
            treeList1.MakeNodeVisible(treeList1.FocusedNode);

        }

In this above code, Move node up and down without having parent and child. in this phase its working fine.

if the user focused root node and click up button that time get the lastnode index value using that index I set the selectednode to the lastnode position even user click up button from root node it will move to last and make that node visible in the treelist.

But if the selected node as a first child node of the particular parent it will not move to the lastchild while clicking up button. How to achieve this? Is this possible to do? Give some idea to do it.


Solution

  • I find out the solution, Here it is,

    C#

    if(treeList1.FocusedNode.ParentNode!=null && foucsednodeindex == 0) // check its child node or not
            {
    
              int parentnodeindex = treeList1.GetNodeIndex(treeList1.FocusedNode.ParentNode);
    
                int foucsedindex = treeList1.GetNodeIndex(treeList1.FocusedNode);
    
                treeList1.SetFocusedNode(treeList1.FocusedNode.ParentNode);
    
                int count = treeList1.FocusedNode.Nodes.Count;
    
    
                treeList1.SetFocusedNode(treeList1.FocusedNode.NextVisibleNode);
    
                 treeList1.SetNodeIndex(treeList1.FocusedNode, count);
                treeList1.MakeNodeVisible(treeList1.FocusedNode);
    
            }