拖曳在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);
       }
}