文件拖放在列表框上不起作用

File drag and drop not working on listbox(文件拖放在列表框上不起作用)
本文介绍了文件拖放在列表框上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这是我第一次使用拖放.所以我有一个带有 listbox 的表单,仅此而已.我希望能够将文件从桌面或 Windows 资源管理器拖放到我的列表框中.这是我的代码.缺少什么?

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?

表格:

 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:(初始化组件)

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);
}

推荐答案

我做了这个,我想这样就可以了.而且不需要 DragOver.

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]));
    }

这篇关于文件拖放在列表框上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)