-1

这是代码。我试图在我的硬盘上标记 40 个文件,然后我将它们拖到列表框,但它只添加了一个文件。我想在拖动它们时将所有选定的文件添加到我的硬盘上,我该怎么做?

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;

        }
4

2 回答 2

0

更改您的代码,以便首先您可以摆脱 break 语句,并在 for 循环中获取所有内容,(为了您自己的方便,将其更改为 foreach 循环)

假设文件数组有多个条目,那么您需要获取这些并将它们作为文件信息添加到文件信息列表中,然后您可以将其绑定到您的下拉列表

我没有拖放事件的经验,但我认为你应该处理拖放事件而不是这里的拖放检查http://msdn.microsoft.com/en-us/library/system.windows.dragdrop.aspx

像这样:

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;
}
于 2012-08-21T14:50:20.067 回答
0

解决了:

我还需要使用事件 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;

        }

这样我可以拖动一个文件或一组文件,它会添加它们。谢谢。

于 2012-08-21T17:26:24.593 回答