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:
- Set the allowdrop attribute of ListBox to true.
- 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?