How to implement file drag-and-drop Open in C #-Basic application

Source: Internet
Author: User

C # implementation file drag and drop and open file

Two events to be aware of the listbox: A DragEnter event occurs when you drag an object within the bounds of the control;

The object you are dragging is not the object you want to put on the control. You need to handle this event when you drag one or more files onto the control. This makes

You have to be able to display the corresponding icon based on the object you are dragging when you drag the object over the control. Occurs when the dragged object is released onto the control.

DragDrop event.

Feature Description: Drag a file into the ListBox, the listbox displays the path to the file, click the path, and click the Open button to open the file.



Code implementation:

You need to change the AllowDrop property of the listbox to true and implement its DragEnter, DragDrop these two events.

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;

Namespace DragDrop
{
public partial class Dragdrop:form
{
public string FilePath;

Public DragDrop ()
{
InitializeComponent ();
}

<summary>
Gets the value of the listbox.
</summary>
<returns></returns>
public string Getlistboxitem ()
{
String FilePath = String. Empty;

BOOL isselected = islistboxselected ();

if (isselected==true)
{
String listboxitemvalue = LbFilePath.SelectedItem.ToString ();

FilePath = Listboxitemvalue;
}
Else
{
MessageBox.Show ("ListBox must be selected.");
}

return filePath;
}

       ///<summary>
       / Whether the value in the ListBox is selected.
       ///</summary>
        ///<returns></returns>
        public bool islistboxselected ()
        {
             bool selected;

if (Lbfilepath.selectedindex = = 1)//selectedindex==-1, indicates that no item is selected.
{
selected = false;
}
Else
{
selected = true;
}

return selected;
}

        private void Lbfilepath_dragenter (object sender, DragEventArgs e)
        {
             if (e.data.getdatapresent (dataformats.filedrop))
             {
                 e.effect = DragDropEffects.All;
           }
            Else
             {
                 e.effect = DragDropEffects.None;
           }
       }

private void Lbfilepath_dragdrop (object sender, DragEventArgs e)
{
String[] s= (string[]) e.data.getdata (dataformats.filedrop,false);

for (int i = 0; i < s.length; i++)
{
LBFILEPATH.ITEMS.ADD (S[i]);
}
}

private void Btnopenfile_click (object sender, EventArgs e)
{
String Filepath=getlistboxitem ();

if (!string. IsNullOrEmpty (FilePath))
{
System.Diagnostics.Process.Start (FilePath);
}
}
}
}

Summarize:

The GetData method of the Data object returns an array of strings that contains the full pathname of the file dragged to the list box control. You can use this file path information to perform any action that needs to be performed on the file.

Supplemental Code:

Copy Code code as follows:
private void Form1_dragenter (object sender, System.Windows.Forms.DragEventArgs e)
{
if (E.data.getdatapresent (DataFormats.FileDrop))
E.effect = Dragdropeffects.link;
else e.effect = DragDropEffects.None;
}

private void Form1_dragdrop (object sender, System.Windows.Forms.DragEventArgs e)
{
Which Label1. Text shows the file name dragged into the document;
Label1. Text = ((System.Array) e.Data.GetData (DataFormats.FileDrop)). GetValue (0). ToString ();
}

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.