Home>
public form1 ()
{
 initializecomponent ();
 treeview1.allowdrop=true;
 treeview1.itemdrag +=new itemdrageventhandler (treeview1_itemdrag);
 treeview1.dragenter +=new drageventhandler (treeview1_dragenter);
 treeview1.dragdrop +=new drageventhandler (treeview1_dragdrop);
}
void treeview1_dragdrop (object sender, drageventargs e)
{
 treenode movenode=(treenode) e.data.getdata ("system.windows.forms.treenode");
 //Determine the target node to move to according to the mouse coordinates
 point pt;
 treenode targenode;
 pt=((treeview) (sender)). pointtoclient (new point (e.x, e.y));
 targenode=this.treeview1.getnodeat (pt);
 //If the target node has no children, it is added as a peer node,Conversely added to the end of lower nodes
 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 currently selected node selection
 treeview1.selectednode=newmovenode;
 //Expand the target node,Easy to display drag and drop effects
 targenode.expand ();
 //Remove the dropped node
 movenode.remove ();
}
void treeview1_dragenter (object sender, drageventargs e)
{
 if (e.data.getdatapresent ("system.windows.forms.treenode"))
 {
  e.effect=dragdropeffects.move;
 }
 else
 {
  e.effect=dragdropeffects.none;
 }
}
void treeview1_itemdrag (object sender, itemdrageventargs e)
{
 if (e.button == mousebuttons.left)
 {
  dodragdrop (e.item, dragdropeffects.move);
 }
}
c
  • Previous Explain the this pointer and constant object in C ++
  • Next C # method to refresh the desktop