C# 如何将多个或一组选定文件从硬盘文件拖到列表框中?
这是代码。我试着在我的硬盘上标记40个文件,然后我把它们拖到列表框中,但它只添加了一个文件。我想在拖动时将所有选定的文件添加到硬盘上,我该怎么做C# 如何将多个或一组选定文件从硬盘文件拖到列表框中?,c#,C#,这是代码。我试着在我的硬盘上标记40个文件,然后我把它们拖到列表框中,但它只添加了一个文件。我想在拖动时将所有选定的文件添加到硬盘上,我该怎么做 private void listBox1_DragOver(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] file
private void listBox1_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
bool bfound = false;
for (int i = 0; i < files.Length; i++)
{
FileInfo fi = new FileInfo(files[i]);
//add more extensions here
if (fi.Extension == ".jpg" || fi.Extension == ".png" || fi.Extension == ".bmp" || fi.Extension == ".emf" || fi.Extension == ".gif" || fi.Extension == ".ico" || fi.Extension == ".tiff"
|| fi.Extension == ".wmf" || fi.Extension == ".exif")
{
bfound = true;
break;
}
}
if (bfound)
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
else
e.Effect = DragDropEffects.None;
}
private void listBox1\u DragOver(对象发送方,DragEventArgs e)
{
if(例如Data.GetDataPresent(DataFormats.FileDrop))
{
string[]files=(string[])e.Data.GetData(DataFormats.FileDrop);
boolbfound=false;
for(int i=0;i
更改代码,以便首先可以去掉break语句,并将所有内容都放入for循环(为了方便起见,将其更改为foreach循环)
假设文件数组有多个条目,则需要将这些条目作为文件信息添加到文件信息列表中,然后将其绑定到dropdownlist
我没有拖放事件的经验,但我认为你应该处理拖放事件,而不是在这里检查dragover
像这样:
private void listBox1_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
bool bfound = false;
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
//add more extensions here
if (fi.Extension == ".jpg" || fi.Extension == ".png" || fi.Extension == ".bmp" || fi.Extension == ".emf" || fi.Extension == ".gif" || fi.Extension == ".ico" || fi.Extension == ".tiff"
|| fi.Extension == ".wmf" || fi.Extension == ".exif")
{
bfound = true;
}
if (bfound)
{
e.Effect = DragDropEffects.Copy;
}
else
e.Effect = DragDropEffects.None;
}
}
else
e.Effect = DragDropEffects.None;
}
已解决:
我还需要使用事件DragDrop,而不仅仅是DragOver:
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
for (int i = 0; i < files.Length; i++)
{
FileInfo fi = new FileInfo(files[i]);
//add more extensions here
if (fi.Extension == ".jpg" || fi.Extension == ".png" || fi.Extension == ".bmp")
{
//do something with the files
listBox1.Items.Add(fi.FullName);
}
}
}
}
private void listBox1_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
bool bfound = false;
for (int i = 0; i < files.Length; i++)
{
FileInfo fi = new FileInfo(files[i]);
//add more extensions here
if (fi.Extension == ".jpg" || fi.Extension == ".png" || fi.Extension == ".bmp" || fi.Extension == ".emf" || fi.Extension == ".gif" || fi.Extension == ".ico" || fi.Extension == ".tiff"
|| fi.Extension == ".wmf" || fi.Extension == ".exif")
{
bfound = true;
break;
}
}
if (bfound)
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
else
e.Effect = DragDropEffects.None;
}
private void listBox1\u DragDrop(对象发送方,DragEventArgs e)
{
if(例如Data.GetDataPresent(DataFormats.FileDrop))
{
string[]files=(string[])e.Data.GetData(DataFormats.FileDrop);
for(int i=0;i
这样,我可以拖动一个文件或一组文件,它将添加它们。
谢谢。文件数组的内容是什么?其中只有一个文件还是多个文件?另外,break语句在那里做什么,使用它的编码很糟糕,但我也认为这就是你的问题所在。中断将相当于for循环。使用foreach循环而不是for循环可能更容易,也不太容易出错。