How to: Provide file drag-and-drop functionality in Visual C #. NET Applications

Source: Internet
Author: User

This article assumes that you are familiar with the following topics:

    • Windows Forms list Box control
    • Windows Forms Event Handling
Steps to build the sample the list box control provides the two drag-and-drop events you need to handle: DragEnter and DragDrop. The DragEnter event occurs when you drag an object within the bounds of the control, which determines whether the currently dragged object is not the object you want to put on the control. This event needs to be handled when you drag one or more files onto the control. This allows you to display an appropriate icon for the object you are dragging, when you drag the object over the control. The DragDrop event occurs when the dragged object is released to the control. You can retrieve an object by handling this event. The data object is used to retrieve the information.

The GetData method of the Data object returns an array of strings that contain the full pathname of the file dragged to the ListBox control. You can use this file path information to perform any action that needs to be performed on the file. For example, you can use classes in the System.IO namespace to open and read files, move files, or copy files to a new location. In this example, you just added the full path to the file that you dragged into the ListBox control.

To provide file drag-and-drop functionality in a Visual C #. NET application, follow these steps:
  1. Create a new Windows forms application in Visual C #. NET. Form1 is created by default.
  2. Use the tool box to add a list box control to Form1.
  3. In the Properties window, change the AllowDrop property of the list box control to Trueto allow the object to be dragged onto the control.
  4. In Solution Explorer, right-click Form1, and then click View Code .
  5. To handle the DragEnter event, add the following method to the Windows Forms designer under the code snippet that is generated in the Form1 class:
    private void listBox1_DragEnter (object sender, System.Windows.Forms.DragEventArgs e) {if (E.data.getdatapresent ( DataFormats.FileDrop)) E.effect = Dragdropeffects.all;elsee. Effect = DragDropEffects.None;}
  6. To handle the DragDrop event, add the following method to the Form1 class, immediately after the method that you added in step 5th:
    private void listBox1_DragDrop (object sender, System.Windows.Forms.DragEventArgs e) {string[] s = (string[]) e. Data.getdata (DataFormats.FileDrop, false); int i;for (i = 0; i < s.length; i++) ListBox1.Items.Add (S[i]);}
  7. To associate these two event handlers with control events, add the following code to the Form1 InitializeComponent method. Be sure to add this code to the location after the ListBox1 has been instantiated:
    This.listBox1.DragDrop + = new           System.Windows.Forms.DragEventHandler (This.listbox1_dragdrop); This.listBox1.DragEnter + = new           System.Windows.Forms.DragEventHandler (This.listbox1_dragenter);
  8. Build and run the project.
  9. Drag one or more files from the desktop or another folder to the list box control. You will notice that the full path to the file is added to the ListBox control.
Back to the top | Provide feedback reference for more information, see the following Microsoft Developer Network (MSDN) Web site: Control.dragenter Event
Http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragenter (vs.71). aspx

Control.dragdrop Events
Http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragdrop (vs.71). aspx Back to the top | Providing feedback properties Article ID: 307966-last review: May 13, 2007-Revision: 1.1 The information in this article applies to:
    • Microsoft Visual C #. NET 2002 Standard Edition
Key words: kbHowTo Kbhowtomaster KB307966
Microsoft and/or its suppliers make no representations regarding the suitability of the documents published on this server for any purpose and the information contained in the graphics. All such documents and related graphics are provided "as is" without warranties of any kind. Microsoft and/or its suppliers hereby disclaim all warranties and conditions with respect to such information, including all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement. In no event will Microsoft and/or its suppliers be liable for any special, indirect, consequential or any loss of use, data or profits arising out of or in connection with the use or operation of the information on the server or in any proceedings relating to such use or operation, No liability for data or profits.

How to: Provide file drag-and-drop functionality in Visual C #. NET Applications

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.