Drag nodes from the outside to the Tree Structure in winform (Treeview and listview drag each other) (1)

Source: Internet
Author: User

A recent project used to drag an item from listview to Treeview to become a part of the tree structure by dragging the icon from the outside. I summarized some implementation methods and shared them with you. This is an example in winform.

 

Before performing the drag-and-drop operation, you must set the "allowdrop" attribute value of the component to "true" because this attribute determines whether the component can perform the drag-and-drop operation.
Three events are used: "itemdrag", "dragenter", and "dragdrop ". Only the first event is triggered in the source component, and the other two events are triggered in the target component. The "itemdrag" event is triggered when you drag the component, the "dragenter" event is triggered when you drag data into the target component area, and the "dragdrop" event is triggered when you place the dragged data in the target component area.

ImplementationCodeAs follows:

1   Private   Void Listviewdomainitemdrag ( Object Sender, system. Windows. Forms. itemdrageventargs E)
2 {
3 This . Dodragdrop (E. Item, dragdropeffects. Move );
4 }
5
6 Private   Void Listviewdomaindragenter ( Object Sender, system. Windows. Forms. drageventargs E)
7 {
8 E. Effect = Dragdropeffects. move;
9 }
10
11 Private   Void Treeviewincludragenter ( Object Sender, system. Windows. Forms. drageventargs E)
12 {
13 // Determines whether the data being dragged is a listview item.
14 Listviewitem LVI = (Listviewitem) E. Data. getdata ( Typeof (Listviewitem ));
15 If (LVI ! =   Null )
16 {
17 E. Effect = Dragdropeffects. move;
18 }
19 Else  
20 Cursor = Cursors. No;
21
22 }
23 Private   Void Treeviewincludragdrop ( Object Sender, system. Windows. Forms. drageventargs E)
24 {
25 // Get items in drag-and-drop
26 Listviewitem LVI = (Listviewitem) E. Data. getdata ( Typeof (Listviewitem ));
27
28 // Determine the target node to be moved to based on the mouse coordinates
29 Point pt;
30 Treenode targenode;
31 PT = (Treeview) (sender). pointtoclient ( New Point (E. X, E. y ));
32 Targenode =   This . Treeview1.getnodeat (PT );
33 Treenode newmovenode = New Treenode (LVI. Text );
34 If (Targenode ! = Null ) // If the target node is not selected, add it to the first-level node.
35 {
36 Targenode. nodes. Add (newmovenode );
37 // Update the selected node that is being dragged
38 Treeview1.selectednode = Newmovenode;
39 // Expand the target node to show the drag-and-drop effect
40 Targenode. Expand ();
41 Treeview1.refresh ();
42 }
43 Else
44 {
45 This . Treeview1.nodes. Add (newmovenode );
46 // Update the selected node that is being dragged
47 Treeview1.selectednode = Newmovenode;
48 // Expand the target node to show the drag-and-drop effect
49 Treeview1.expandall ();
50 Treeview1.refresh ();
51 }
52
53
54 }

 

Finally, do not forget to add the corresponding event!

In this way, you can easily drag nodes from the external to the tree structure. Insert an image in the next articleArticle.

 

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.