2009-11-17 40 views
3

我們有一個觸發DoDragDrop方法的ListView控件。我們有另一個控件,它是一個具有DragDrop方法的TreeView控件。問題是DragDrop方法的sender參數不是ListView,儘管ListView啓動了DoDragDrop方法。相反,發件人是TreeView本身。任何想法爲什麼發件人不正確?C#在兩個不同的控件之間拖放

+1

這是因爲sender參數是與發送事件的控制(即TreeView控件)不是誰開始拖拽做。 – tyranid 2009-11-17 20:59:15

回答

1

阿馬爾,

爲蟲族說,「發件人」是觸發事件的控制。這個控制從來不是開始拖動的控制,而是接受拖動的控制。

一個例子:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    /// <summary> 
    /// There's button 1 and button 2... button 1 is meant to start the dragging. Button 2 is meant to accept it 
    /// </summary> 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     /// <summary> 
     /// This is when button 1 starts the drag 
     /// </summary> 
     private void button1_MouseDown(object sender, MouseEventArgs e) 
     { 
      this.DoDragDrop(this, DragDropEffects.Copy); 
     } 

     /// <summary> 
     /// This is when button 2 accepts the drag 
     /// </summary> 
     private void button2_DragEnter(object sender, DragEventArgs e) 
     { 
      e.Effect = DragDropEffects.Copy; 
     } 


     /// <summary> 
     /// This is when the drop happens 
     /// </summary> 
     private void button2_DragDrop(object sender, DragEventArgs e) 
     { 
      // sender is always button2 
     } 

    } 
}