文件拖放在列表框不工作 [英] File drag and drop not working on listbox

查看:115
本文介绍了文件拖放在列表框不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次拖放工作。所以我有一个列表框,别无其他形式。我希望能够拖放通过桌面或Windows资源管理器文件到我的列表框。这是我的代码。什么是缺少



表格:



 公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
}

私人无效listBox1_DragEnter(对象发件人,DragEventArgs E)
{
如果(e.Data.GetDataPresent(DataFormats.FileDrop))
é .Effect = DragDropEffects.All;
,否则
e.Effect = DragDropEffects.None;
}


私人无效listBox1_DragDrop(对象发件人,DragEventArgs E)
{
的String [] S =(字符串[])e.Data。的GetData(DataFormats.FileDrop,FALSE);
INT I;
为(i = 0; I< s.Length;我++)
listBox1.Items.Add(S [I]);
}
}



Form1.Designer.cs:(InitializeComponents )

 私人无效的Ini​​tializeComponent()
{
this.listBox1 =新系统。 Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1中
//
this.listBox1.AllowDrop = TRUE;
this.listBox1.FormattingEnabled = TRUE;
this.listBox1.Location =新System.Drawing.Point(30,23);
this.listBox1.Name =listBox1中;
this.listBox1.Size =新System.Drawing.Size(376,238);
this.listBox1.TabIndex = 0;
this.listBox1.DragDrop + =新System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
this.listBox1.DragEnter + =新System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
this.listBox1.DragOver + =新System.Windows.Forms.DragEventHandler(this.listBox1_DragOver);
//
// Form1中
//
this.AutoScaleDimensions =新System.Drawing.SizeF(6F,13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize =新System.Drawing.Size(438,366);
this.Controls.Add(this.listBox1);
this.Name =Form1的;
this.Text =Form1的;
this.ResumeLayout(假);
}


解决方案

我做这个,我想这将是OK.And没有必要的dragover。

 私人无效listBox_DragEnter(对象发件人,DragEventArgs E)
$ { b $ b如果(e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
,否则
e.Effect = DragDropEffects.None;
}

私人无效listBox_DragDrop(对象发件人,DragEventArgs E)
{
如果(listBox.Items.Count!= 0)
{
listBox.Items.Clear();
}
的String [] S =(字符串[])e.Data.GetData(DataFormats.FileDrop,FALSE);
INT I;
为(i = 0; I< s.Length;我++)
listBox.Items.Add(Path.GetFileName(S [I]));
}


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

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆