2016-02-27 35 views
1

我有一個從列表框或DataGrid返回選定的項目的問題。問題是,我特別希望用戶雙擊所選項目,而不是控件上的任何其他位置(與DoubleClick事件相同)。 目前我有這樣的:C#ListBox或DataGrid雙擊SELECTED項

private void listBox1_DoubleClick(object sender, EventArgs e) 
    { 
     if (listBox1.SelectedItem == null) return; 
     System.Console.WriteLine(listBox1.SelectedItem.ToString()); //console for testing 
    } 

雖然它的工作原理,我不喜歡的事實,你可以在一個項目點擊一次,然後雙擊控制的空白處,並返回以前選定的項目。有沒有簡單的方法來改變它?

回答

0

你應該得到其中雙擊事件發生的項目index from the location

int index = listBox1.IndexFromPoint(e.Location); 
if (index != System.Windows.Forms.ListBox.NoMatches) 
{ 
    System.Console.WriteLine(listBox1.SelectedItem.ToString()); //console for testing 
} 
+0

的EventArgs在雙擊任何位置。鼠標事件沒有雙擊事件。我不知道如何實現這個... –