C # simple tree-based Treeview node drag and drop functions)

Source: Internet
Author: User

This article excerpted the cool man in the blog garden. Http://www.cnblogs.com/jirigala

 

ExampleProgramHow is the running effect:

Note the following when dragging a node:

1: it is better to have prompt information when you drag it, so that you cannot find the information to be picked after misoperations.

2: I can't drag my father's day node to my child node. Isn't it an endless loop or a mess?

To enable the Treeview to support the drag and drop function, pay attention to the following attribute settings and corresponding events:Code.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The code implementation is as follows:

Private Void Tvorganize_itemdrag ( Object Sender, itemdrageventargs E)
{
If ( This . Permissionedit)
{
// Start the drag-and-drop operation, and set the drag-and-drop effect to move.
This . Dodragdrop (E. Item, dragdropeffects. Move );
}
}

Private VoidTvorganize_dragenter (ObjectSender, drageventargs E)
{
//Drag effect to move
E. effect = dragdropeffects. move;
}

Private Void Tvorganize_dragdrop (Object Sender, drageventargs E)
{
// Define an intermediate variable
Treenode;
// Determines whether the drag type is treenode. If not, it will not be processed.
If (E. Data. getdatapresent ( " System. Windows. Forms. treenode " , False ))
{
// Drag-and-drop target node
Treenode targettreenode;
// Obtains the coordinates of the current cursor.
// Defines the variable of a position and saves the coordinate point of the current cursor.
Point point = (Treeview) sender). pointtoclient ( New Point (E. X, E. y ));
// Obtain the node at the coordinate point based on the coordinate point
Targettreenode = (Treeview) sender). getnodeat (point );
// Get the dragged Node
Treenode = (treenode) E. Data. getdata (" System. Windows. Forms. treenode " );
// Determine whether the dragged node is the same as the target node.
If (Baseinterfacelogic. treenodecanmoveto (treenode, targettreenode ))
{
If (Basesysteminfo. showinformation)
{
// Move team?
If (MessageBox. Show (appmessage. Format (appmessage. msg0038, treenode. Text, targettreenode. Text), appmessage. msg0000, response. yesno, messageboxicon. Question) = dialogresult. No)
{
Return ;
}
}
Servicemanager. instance. organizeservice. moveTo (userinfo, treenode. Tag. tostring (), targettreenode. Tag. tostring ());
// Add a clone of the dragged node to the target node.
Targettreenode. nodes. Add (treenode) treenode. Clone ());
// Remove the dragged Node
Treenode. 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.