拖曳在Windows App中放入列表框 [英] Drag & Drop in ListBox in Windows App

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

问题描述

在我的Windows应用程序中,我在表单上有一个列表框.我想要的是,当我从资源管理器中将单个或多个文件拖放到列表框中时,文件的绝对路径应添加到列表框中.

但是问题是不支持拖动操作.我所希望的只是某种方式使该事件被捕获,并且我应该能够访问在列表框中拖动的文件的名称.将触发dragover,dragenter和dragleave事件,但是无法从"e"中检索到有用的信息. '事件的参数.

请帮我实现这个.

如果列表框和RichTextbox无法实现,那么即使这样也可以.

框架-.net 3.5
软件-Visual Studio 2008

In my windows application i have a listbox on the form. What i want is that when i drag and drop single or multiple files on listbox from the explorer, the absolute paths of the files should get added in the listbox.

But the problem is that the drag operation is not being supported. All i want is that somehow the event gets trapped and i should be able to access the names of the files draged in the listbox.The dragover, dragenter and dragleave events are being fired but no useful information can be retrieved from the ''e'' parameter of the event.

Please help me implement this.

And if it is not possible with the listbox and with a richtextbox, even that will work.

Framework- .net 3.5
Software- Visual Studio 2008

推荐答案

看看这篇文章:
将文件从Windows资源管理器拖放到Windows窗体 [
Have a look at this article:
Drag and Drop files from Windows Explorer to Windows Form[^]

Good luck!


public Form1
{
    InitializeComponent();
    listBox1.AllowDrop = true;
    listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter);
    listbox1.DragDrop += new DragEventHandler(listBox1_DragDrop);
}

private void listBox1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}

private void listBox1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
       {
           string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, true);
           listBox1.Items.Add(fileName);
       }
}


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

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