Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

c# - File drag and drop not working on listbox

This is the first time I am working with drag and drop. So I have a form with a listbox and nothing else. I would like to be able to drag and drop files from desktop or windows explorer into my listbox. This is my code. What is missing?

Form:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;
            else
                e.Effect = DragDropEffects.None;
        }


        private void listBox1_DragDrop(object sender, 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]);
        }
    }

Form1.Designer.cs: (InitializeComponents)

private void InitializeComponent()
{
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    // 
    // listBox1
    // 
    this.listBox1.AllowDrop = true;
    this.listBox1.FormattingEnabled = true;
    this.listBox1.Location = new System.Drawing.Point(30, 23);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(376, 238);
    this.listBox1.TabIndex = 0;
    this.listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
    this.listBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
    this.listBox1.DragOver += new System.Windows.Forms.DragEventHandler(this.listBox1_DragOver);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(438, 366);
    this.Controls.Add(this.listBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I make this and i think this will be OK.And no need DragOver.

    private void listBox_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.All;
        else
            e.Effect = DragDropEffects.None;
    }

    private void listBox_DragDrop(object sender, DragEventArgs e)
    {
        if (listBox.Items.Count != 0)
        {
            listBox.Items.Clear();
        }
        string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
        int i;
        for (i = 0; i < s.Length; i++)
            listBox.Items.Add(Path.GetFileName(s[i]));
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...