2012-01-12 85 views
0

在windows窗體我有ListView和FlowLayoutPanel的拖放不起作用

我想從列表視圖中拖動列表視圖到FlowLayoutPanel中這樣

我使用DragEnter事件

private void listViewGUI_DragEnter(object sender, DragEventArgs e) 
     { 
} 

和在FlowLayoutPanel中激活DragDrop

private void fpnlDisplayedGUI_DragDrop(object sender, DragEventArgs e) 
     { 
} 

問題在於它沒有「他們的牛逼工作不輸入任何此事件,任何想法如何使他們回車我錯過任何財產

問候

回答

2

下面是一個簡單的例子來說明基礎你需要什麼

public Form1() 
{ 
    InitializeComponent(); 

    panel1.MouseDown += new MouseEventHandler(panel1_MouseDown); 
    panel2.AllowDrop = true; 
    panel2.DragEnter += new DragEventHandler(panel2_DragEnter); 
    panel2.DragDrop += new DragEventHandler(panel2_DragDrop); 
} 

void panel2_DragDrop(object sender, DragEventArgs e) 
{ 
    //handle the drop here. 
} 

void panel2_DragEnter(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Move; 
} 

void panel1_MouseDown(object sender, MouseEventArgs e) 
{ 
    panel1.DoDragDrop("whatever you want draged.", DragDropEffects.Move); 
} 
2

您將DragEnter事件放在錯誤的控件上,您必須使用面板。我想我知道你是如何陷入這種麻煩的,ListView沒有任何事件表明用戶開始拖動一個項目。你需要自己綜合。基本的方法是記錄鼠標向下的位置並使用MouseMove事件來檢查用戶是否將鼠標移動得足夠遠以開始拖動。像這樣:

private Point dragMousePos; 

    private void listView1_MouseDown(object sender, MouseEventArgs e) { 
     if (e.Button == MouseButtons.Left) dragMousePos = e.Location; 
    } 

    private void listView1_MouseMove(object sender, MouseEventArgs e) { 
     if (e.Button == MouseButtons.Left) { 
      int dx = Math.Abs(e.X - dragMousePos.X); 
      int dy = Math.Abs(e.Y - dragMousePos.Y); 
      if (dx >= SystemInformation.DoubleClickSize.Width || 
       dy >= SystemInformation.DoubleClickSize.Height) { 
       var item = listView1.GetItemAt(dragMousePos.X, dragMousePos.Y); 
       if (item != null) listView1.DoDragDrop(item, DragDropEffects.Move); 
      } 
     } 
    }