Winform implements simple drag and drop functions (C #)

Source: Internet
Author: User

Two controls, ListBox and Treeview, are used as the data source. You can drag the data to the Treeview to automatically add a tree node.

The ListBox control mousedown is used to obtain the value to be dragged and call the dodragdrop method.

Private   Void Listbox1_mousedown ( Object Sender, mouseeventargs E)
{
// Call the dodragdrop Method
If ( This . Listbox1.selecteditem ! =   Null )
{
This . Listbox1.dodragdrop ( This . Listbox1.selecteditem, dragdropeffects. copy );
}
}

 

The dragenter and dragdrop events of the Treeview control are used to receive data and add it as a tree node.

Private   Void Treeviewincludragenter ( Object Sender, drageventargs E)
{
// Set the drag type (copy and drag here)
E. Effect = Dragdropeffects. copy;
}

Private   Void Treeviewincludragdrop ( Object Sender, drageventargs E)
{
// Get value
String Item = ( String ) E. Data. getdata (E. Data. getformats ()[ 0 ]);

This. Treeview1.nodes. Add (item );
}

 

/Files/gossip/winformdraganddrop.rar source code download

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.