C# TreeView Move Note

  • 2781
  • 0
  • 2013-09-25

C# TreeView Move Note

 

使用前:

Root
--1
----11
----12
----13
----14
--2
etc...

 

使用後,13不見了~~

Root
--1
----11
----12
----14
----13
--2
etc...

 

TreeNodeMenu tnmNode = tvwMenuitemSup.SelectedNode;
TreeNode tnmPreviousNode =tnmNode.PrevNode;
 
if (tnmPreviousNode != null)
{
    int idxBegin = tnmNode.Index;
    int idxEnd = tnmPreviousNode.Index;
    
    TreeNode tnmNodeParent = tnmNode.Parent;
    if (tnmNodeParent != null)
    {
        tnmNode.Remove();
        tnmPreviousNode.Remove();
        tnmNodeParent.Nodes.Insert(idxBegin, tnmPreviousNode);
        tnmNodeParent.Nodes.Insert(idxEnd, tnmNode);
    }
}