I have worked on a Windows form and need to implement the drag and drop function in the Treeview space. First, I would like to introduce the Treeview control in Windows form: using the Treeview control in Windows Forms, you can display the node hierarchy for users, just like displaying files and folders in the left pane of the Windows resource manager function of Windows operating system. Each node in the tree view may contain other nodes, which are called "subnodes ". You can expand or collapse a node to display a parent node or a node that contains a child node. You can set the checkboxes attribute of the Tree View to true and display the tree view with check boxes next to the node. Then, you can select or clear a node programmatically by setting the checked attribute of the node to true or false. Through the above instructions, you may have a preliminary understanding of the Treeview control. In this article, I want to show you how to drag and drop contacts in the Treeview, how to drag a node to another node to make it a child node of the target node, and the child nodes of the dragged node will be dragged to the target node together, and maintain its original tree structure.
To fully display nodes during form load, you do not need to open them one by one. We recommend that you add the followingCode:
Private Void Frmdrag_load ( Object Sender, eventargs E)
{
This . Treeview1.expandall ();
}
To implement the drag and drop function of Treeview, we must first set its allowdrop attribute to true, so that the contacts of the Treeview can be dragged. Next we will add three events for the Treeview, which are 1. itemdrag events (when the user starts to drag the node .) 2. dragenter event (this event occurs when an object is dragged into the control's border .) 3. dragdrop event (this event occurs when the drag-and-drop operation is completed .)
Note: 1. The target node cannot be empty. 2. The target node cannot be dragged. 3. The target node cannot be the byte of the dragged node. The code for the three events is as follows:
Private Point Position = New Point ( 0 , 0 );
Private VoidTreeviewincluitemdrag (ObjectSender, itemdrageventargs E)
{
Dodragdrop (E. Item, dragdropeffects. Move );
}
Private Void Treeviewincludragenter ( Object Sender, drageventargs E)
{
If (E. Data. getdatapresent ( Typeof (Treenode )))
E. Effect = Dragdropeffects. move;
Else
E. Effect = Dragdropeffects. None;
}
Private Void Treeviewincludragdrop ( Object Sender, drageventargs E)
{
Treenode mynode = Null ;
If (E. Data. getdatapresent ( Typeof (Treenode )))
{
Mynode = (Treenode) (E. Data. getdata ( Typeof (Treenode )));
}
Else
{
MessageBox. Show ( " Error " );
}
Position. x = E. X;
Position. Y = E. Y;
Position = Treeview1.pointtoclient (position );
Treenode dropnode = This . Treeview1.getnodeat (position );
// 1. The target node is not empty. 2. The target node is not the byte point of the dragged contact. 3. The target node is not a dragged node.
If (Dropnode ! = Null && Dropnode. Parent ! = Mynode && Dropnode ! = Mynode)
{
Treenode dragnode = Mynode;
// The dragged node is deleted from the original location.
Mynode. Remove ();
// Add a dragged node to the target node
Dropnode. nodes. Add (dragnode );
}
// If the target node does not exist, that is, if the drag position does not exist, the dragged node is placed under the root node.
If (Dropnode = Null )
{
Treenode dragnode = Mynode;
Mynode. Remove ();
Treeview1.nodes. Add (dragnode );
}
}
Now you can run it and check the pull effect. It feels pretty good, huh, huh.