C#的ListView DragDrop事件方法执行两次每一滴水 [英] C# ListView DragDrop Event Method Executing Twice Per Drop

查看:307
本文介绍了C#的ListView DragDrop事件方法执行两次每一滴水的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该方法连续执行两次而且也没有明显的理由这样做。
它发生在VS2010防爆preSS(4.0)和VS2008(3.5)。

 公共GUI()
{
    的InitializeComponent();
    this.lvwFiles.DragDrop + =新System.Windows.Forms.DragEventHandler(this.lvwFiles_DragDrop);
    this.lvwFiles.DragEnter + =新System.Windows.Forms.DragEventHandler(this.lvwFiles_DragEnter);
}
私人无效lvwFilesAdd(路径字符串,字符串[]路径)
{...}
私人无效lvwFilesWrite()
{...}
私人无效lvwFiles_DragEnter(对象发件人,DragEventArgs E)
{
    如果(e.Data.GetData present(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy;
    其他
        e.Effect = DragDropEffects.None;
}
私人无效lvwFiles_DragDrop(对象发件人,DragEventArgs E)
{
    如果(e.Data.GetData present(DataFormats.FileDrop))
    {
        VAR路径=(字符串[])e.Data.GetData(DataFormats.FileDrop);
        变种路径= Path.GetDirectoryName(路径[0]);
        lvwFilesAdd(通路,路径);
        lvwFilesWrite();
    }
}


解决方案

我在下面的Microsoft例子并没有注意到,在GUI.Designer.cs声明(自动,由IDE)和GUI.cs(手动,来自实施例)是冗余的。

  === GUI.cs ===
公共GUI()
{
    的InitializeComponent();
    this.lvwFiles.DragDrop + =新System.Windows.Forms.DragEventHandler(this.lvwFiles_DragDrop);
    this.lvwFiles.DragEnter + =新System.Windows.Forms.DragEventHandler(this.lvwFiles_DragEnter);
}=== GUI.Designer.cs ===
//
// lvwFiles
//
...
this.lvwFiles.DragDrop + =新System.Windows.Forms.DragEventHandler(this.lvwFiles_DragDrop);
this.lvwFiles.DragEnter + =新System.Windows.Forms.DragEventHandler(this.lvwFiles_DragEnter);

The method executes twice in a row and there's no apparent reason for doing so. It happens in VS2010 Express (4.0) and in VS2008 (3.5).

public GUI()
{
    InitializeComponent();
    this.lvwFiles.DragDrop += new System.Windows.Forms.DragEventHandler(this.lvwFiles_DragDrop);
    this.lvwFiles.DragEnter += new System.Windows.Forms.DragEventHandler(this.lvwFiles_DragEnter);
}  
private void lvwFilesAdd(string path, string[] paths)
{ ... }  
private void lvwFilesWrite()
{ ... }  
private void lvwFiles_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
}  
private void lvwFiles_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
        var path = Path.GetDirectoryName(paths[0]);
        lvwFilesAdd(path, paths);
        lvwFilesWrite();
    }
}

解决方案

I was following Microsoft example and didn't notice that the declarations in GUI.Designer.cs (automatic, by IDE) and in GUI.cs (manual, from example) are redundant.

=== GUI.cs ===
public GUI()
{
    InitializeComponent();
    this.lvwFiles.DragDrop += new System.Windows.Forms.DragEventHandler(this.lvwFiles_DragDrop);
    this.lvwFiles.DragEnter += new System.Windows.Forms.DragEventHandler(this.lvwFiles_DragEnter);
}

=== GUI.Designer.cs ===
// 
// lvwFiles
//
... 
this.lvwFiles.DragDrop += new System.Windows.Forms.DragEventHandler(this.lvwFiles_DragDrop);
this.lvwFiles.DragEnter += new System.Windows.Forms.DragEventHandler(this.lvwFiles_DragEnter);

这篇关于C#的ListView DragDrop事件方法执行两次每一滴水的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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