. Net C # Drag the node of the Treeview, and move the node up and down

Source: Internet
Author: User
Define a Treeview named tvdepartment and set allowdrop to true. // MobileCode

Private   Void Mimoveup_click ( Object Sender, eventargs E) // Move up
{
// By Lei Email: Landgis@126.com, yanleigis@21cn.com 2007.10.10
Treenode Node = Tvdepartment. selectednode;
Treenode prevnode = Node. prevnode;
If (Prevnode ! =   Null )
{

Treenode newnode = (Treenode) node. Clone ();
If (Node. Parent =   Null )
{
Tvdepartment. nodes. insert (prevnode. index, newnode );
}
Else
{
Node. Parent. nodes. insert (prevnode. index, newnode );
}
Node. Remove ();
Tvdepartment. selectednode = Newnode;


}


}
Private   Void Mimovedown_click ( Object Sender, eventargs E) // Move down
{
// By Lei Email: Landgis@126.com, yanleigis@21cn.com 2007.10.10
Treenode Node = Tvdepartment. selectednode;
Treenode nextnode = Node. nextnode;
If (Nextnode ! =   Null )
{

Treenode newnode = (Treenode) node. Clone ();
If (Node. Parent =   Null )
{
Tvdepartment. nodes. insert (nextnode. Index+ 1, Newnode );
}
Else
{
Node. Parent. nodes. insert (nextnode. Index+ 1, Newnode );
}
Node. Remove ();
Tvdepartment. selectednode = Newnode;


}
}
// Drag code
Private   Void Tvdepartment_itemdrag ( Object Sender, itemdrageventargs E) // Left-click drag
{
If(E. Button=Mousebuttons. Left)
{
Dodragdrop (E. Item, dragdropeffects. Move );
}

}

Private   Void Tvdepartment_dragenter ( Object Sender, drageventargs E)
{
If (E. Data. getdatapresent ( " System. Windows. Forms. treenode " ))
{
E. Effect=Dragdropeffects. move;
}
Else
{
E. Effect=Dragdropeffects. None;
}

}

Private   Void Tvdepartment_dragdrop ( Object Sender, drageventargs E) // Drag
{
// By Lei Email: Landgis@126.com, yanleigis@21cn.com 2007.10.10
// Get nodes in drag-and-drop
Treenode movenode = (Treenode) E. Data. getdata ( " System. Windows. Forms. treenode " );

// Determine the target node to be moved to based on the mouse coordinates
Point pt;
Treenode targenode;
PT = (Treeview) (sender). pointtoclient ( New Point (E. X, E. y ));
Targenode =   This . Tvdepartment. getnodeat (PT );

// If the target node has no child nodes, it is added as a peer node, and vice versa.
Treenode newmovenode = (Treenode) movenode. Clone ();
If (Targenode. nodes. Count =   0 )
{
Targenode. Parent. nodes. insert (targenode. index, newmovenode );
}
Else
{
Targenode. nodes. insert (targenode. nodes. Count, newmovenode );
}
// Update the selected node that is being dragged
Tvdepartment. selectednode = Newmovenode;
// Expand the target node to show the drag-and-drop effect
Targenode. Expand ();

// Remove drag-and-drop nodes
Movenode. Remove ();


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.