In C #, let ListBox support drag-and-drop dragdrop of file paths.

Source: Internet
Author: User

Sometimes, our program needs to load a file list, which is usually stored in ListBox. In order to facilitate the operation, it is a very good function to allow ListBox to support dragging to the file path, in. net, It is very convenient and easy to implement, only three steps are required:

  1. Set the allowdrop attribute of ListBox to true.
  2. Handling in the dragover and dragenter events of ListBox
Void listbox1dragenter (Object sender, drageventargs e) {e. effect = dragdropeffects. All;} void listbox1dragover (Object sender, drageventargs e) {e. effect = dragdropeffects. All ;}

The above E. effect can adjust different values according to the actual situation. However, if you are not sure, use the default all

3. process drag-and-drop events

Void listbox1dragdrop (Object sender, drageventargs e) {If (E. data. getdatapresent (dataformats. filedrop, false) {string [] files = (string []) E. data. getdata (dataformats. filedrop); foreach (string s in files) {(sender as ListBox ). items. add (s );}}

This is done, haha.

Is it easy?

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.