Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件直接拖到窗体而不是控件上_C#_Winforms_Drag And Drop - Fatal编程技术网

C# 将文件直接拖到窗体而不是控件上

C# 将文件直接拖到窗体而不是控件上,c#,winforms,drag-and-drop,C#,Winforms,Drag And Drop,是否可以将文件直接拖到windows窗体上,还是必须拖到windows窗体上的控件上?我已经使用下面的代码很长时间了,但这需要您拖到列表视图上 public Form1() { InitializeComponent(); this.Load += new EventHandler(Form1_Load); } void Form1_Load(object sender, EventArgs e) { this.listView1.AllowDrop = true; this.l

是否可以将文件直接拖到windows窗体上,还是必须拖到windows窗体上的控件上?我已经使用下面的代码很长时间了,但这需要您拖到
列表视图上

public Form1()
{
  InitializeComponent();
  this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
  this.listView1.AllowDrop = true;
  this.listView1.Columns.Add("File name");
  this.listView1.Dock = DockStyle.Fill;
  this.listView1.SmallImageList = this.imageList1;
  this.listView1.View = View.Details;
  this.listView1.DragEnter += new DragEventHandler(listView1_DragEnter);
  this.listView1.DragDrop += new DragEventHandler(listView1_DragDrop);
}
void listView1_DragEnter(object sender, DragEventArgs e)
{
  if (e.Data.GetDataPresent("FileDrop") &&
      (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
    e.Effect = DragDropEffects.Copy;
}
}

void listView1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent("FileDrop") &&
    (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        string[] filePaths = (string[])e.Data.GetData("FileDrop");
        ListViewItem[] items = new ListViewItem[filePaths.Length];
        string filePath;
        for (int index = 0; index < filePaths.Length; index++)
        {
          filePath = filePaths[index];
          if (!this.imageList1.Images.Keys.Contains(filePath))
          {
            this.imageList1.Images.Add(filePath,
            Icon.ExtractAssociatedIcon(filePath));
          }
        items[index] = new ListViewItem(filePath, filePath);
    }
      this.listView1.Items.AddRange(items);
  }
}
public Form1()
{
初始化组件();
this.Load+=新的EventHandler(Form1\u Load);
}
void Form1\u加载(对象发送方,事件参数e)
{
this.listView1.AllowDrop=true;
this.listView1.Columns.Add(“文件名”);
this.listView1.Dock=DockStyle.Fill;
this.listView1.SmallImageList=this.imageList1;
this.listView1.View=View.Details;
this.listView1.DragEnter+=新的DragEventHandler(listView1\u DragEnter);
this.listView1.DragDrop+=新的DragEventHandler(listView1u DragDrop);
}
无效列表视图1_DragEnter(对象发送方,DragEventArgs e)
{
if(例如Data.GetDataPresent(“FileDrop”)&&
(e.AllowedEffects和DragDropEffects.Copy)=DragDropEffects.Copy)
{
e、 效果=DragDropEffects.Copy;
}
}
无效列表视图1_DragDrop(对象发送器,DragEventArgs e)
{
if(例如Data.GetDataPresent(“FileDrop”)&&
(e.AllowedEffects和DragDropEffects.Copy)=DragDropEffects.Copy)
{
string[]filepath=(string[])e.Data.GetData(“FileDrop”);
ListViewItem[]items=新ListViewItem[filepath.Length];
字符串文件路径;
for(int index=0;index
Sure:
表单
继承自
控件
,所有
DragDrop
etc事件都在该控件中声明。只需将处理程序添加到表单中,而不是列表框中。的可能重复项,并确保设置
this.AllowDrop=true
this.DragDrop += new DragEventHandler(form_DragDrop);