2013-02-09 88 views
1

我有與下列事件的WinForms DataGridView控件拖動「行」出來的DataGrid:的DataGridView CellDrag太敏感

private void gridOperations_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
     { 

      if (Math.Abs(e.X - mouseDownPos.X) >= SystemInformation.DoubleClickSize.Width || Math.Abs(e.Y - mouseDownPos.Y) >= SystemInformation.DoubleClickSize.Height) 
      { 
       string[] filesToDrag = { "tmp/generated.log" }; 
       gridOperations.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy); 
      } 
     } 
    } 

的問題是我也有datagridview的單擊和雙擊事件,雙點擊事件幾乎從來沒有得到執行,除非我點擊時根本不移動鼠標。我怎麼添加一個'thershold',這樣如果我按住鼠標並拖動單元格3個像素,那麼它會觸發gridOperations.DoDragDrop?謝謝!

回答

1

嘗試使用整數計數器。 每次事件被觸發,你遞增INT,如果它是你到達你treshold執行代碼的其餘部分,其重置爲0。

,如:

private int thCount = 0; 
    private void gridOperations_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     if ((e.Button & MouseButtons.Left) == MouseButtons.Left && thCount==5) 
     { 
      //... 
      thCount = 0; 
     } 
     else 
     { 
      thCount++; 
     } 
    } 

希望我可以幫助你

+0

非常感謝,完美的工作! – Kristian 2013-02-11 12:02:28