简体   繁体   English

C#ListView DragDrop事件方法每次执行两次

[英]C# ListView DragDrop Event Method Executing Twice Per Drop

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). 它发生在VS2010 Express(4.0)和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. 我正在关注Microsoft示例,并没有注意到GUI.Designer.cs(自动,由IDE)和GUI.cs(手动,来自示例)中的声明是多余的。

=== 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM