Quick implementation of the drag-and-drop Treeview control (C #) in Windows Applications #)

Source: Internet
Author: User

Summary

You can quickly implement the drag-and-drop Treeview control in a Windows application.

Technical description

Treeview provides drag-and-drop events:


Itemdrag: triggered when you drag treenode. When it occurs, call the dodragdrop method to initialize the drag process.
 

Dragenter: After you initialize the drag operation, you must process the dragenter event of the target Treeview control. This event occurs when you drag a treenode object from the Treeview control to the target control. The dragenter event can specify the target Treeview control, whether or not the control is used by the drag operation. The Code only involves mobile operations.
 

Dragdrop: Finally, the dragdrop event of the target Treeview is to be processed. This event occurs when you drag the treenode object and release it to the target control. Process this event, return the treenode object, and add it to the target Treeview control. Return data in the code.
 

Run the following code to drag any node on a Treeview control to a specified node. You can also expand the programs that the treenode drag between multiple Treeview controls. The getdata method of the Data Object returns the dragged treenode object. The getnodeat method is used to determine the target control to which the treenode object is dragged (this is the target treenode object ). After determining the location, add the source treenode object to the target treenode object as its subnode. Because it is a mobile operation, the source treenode object will be deleted at last.

Follow these steps to create an instance program:

1.
Create a C # Windows Application
 
2.
Added a Treeview control to the interface.
 
3.
Set the allowdrop attribute of Treeview to true.
 
4.
Add the following code to the page_load method:

 

Private void form1_load (Object sender, system. eventargs E)

{

// Add some test nodes to the Treeview Control

Treenode parentnode1;

Parentnode1 = treeview1.nodes. Add ("TV1 ");

Parentnode1.nodes. Add ("tv1firstchild ");

Parentnode1.nodes. Add ("tv1secondchild ");

Parentnode1.nodes. Add ("tv1thirdchild ");

Parentnode1.nodes. Add ("tv1fourthchild ");

Parentnode1.expand ();

 

// Add events to the Treeview Control

This. treeview1.itemdrag + = new system. Windows. Forms. itemdrageventhandler (this. treeview_itemdrag );

This. treeview1.dragenter + = new system. Windows. Forms. drageventhandler (this. treeview_dragenter );

This. treeview1.dragdrop + = new system. Windows. Forms. drageventhandler (this. treeview_dragdrop );

}
 
5.
// Treeview_itemdrag Event code:

Private void treeview_itemdrag (Object sender,

System. Windows. Forms. itemdrageventargs E)

{

Dodragdrop (E. Item, dragdropeffects. Move );

}
 
6.
// Treeview_dragenter Event code:

Private void treeview_dragenter (Object sender,

System. Windows. Forms. drageventargs E)

{

E. effect = dragdropeffects. move;

}
 
7.
 

// Treeview_dragdrop Event code:

Private void treeview_dragdrop (Object sender, system. Windows. Forms. drageventargs E)

{

Treenode newnode;

 

If (E. Data. getdatapresent (typeof (treenode )))

{

Point Pt = (Treeview) sender). pointtoclient (new point (E. X, E. y ));

Treenode destinationnode = (Treeview) sender). getnodeat (PT );

Newnode = (treenode) E. Data. getdata ("system. Windows. Forms. treenode ");

Destinationnode. nodes. Add (treenode) newnode. Clone ());

Destinationnode. Expand ();

// Delete a moved Node

Newnode. Remove ();

}

}
 

 


 

Related Article

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.