2010-01-16 28 views
6

Phương thức thực hiện hai lần liên tiếp và không có lý do rõ ràng nào để thực hiện điều đó. Nó xảy ra trong VS2010 Express (4.0) và trong VS2008 (3.5).C# ListView DragDrop Phương thức sự kiện Thực hiện hai lần cho mỗi lần thả

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(); 
    } 
} 
+3

Bạn có chắc chắn rằng 'InitializeComponent()' không cài đặt trình xử lý kéo thả không? –

+1

Chỉ cần lặp lại nhận xét của J. Knoeller ở trên: mở tệp Designer.cs và kiểm tra trình xử lý DragEnter và DragDrop bổ sung đang có. – BillW

+0

Ok, đã hiểu, cảm ơn! – OIO

Trả lời

4

Tôi đã theo dõi ví dụ của Microsoft và không nhận thấy rằng các khai báo trong GUI.Designer.cs (tự động, bởi IDE) và trong GUI.cs (thủ công, ví dụ) là thừa.

=== 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ác vấn đề liên quan