2010-06-17 98 views
1

我正在使用in-place editable listview控件的項目。防止ListView中的ItemChecked事件干擾SubItemClicked使用C#

可編輯列表視圖添加'SubItemClicked'事件,以便可以編輯每個「單元格」。

lstSD2.SubItemClicked += new ListViewEx.SubItemEventHandler(lstSD2_SubItemClicked); 

我也有一個'ItemChecked'事件啓用listview複選框。

問題是,一旦'ItemChecked'事件被啓用,雙擊任何行將觸發'ItemChecked'事件並阻止'SubItemClicked'事件觸發。

有沒有一種方法來強制實際檢查listview複選框,而不是每當雙擊該行時觸發?

一個可能的解決辦法是禁用ListView的「DoubleClickActivation」:

this.lstShuntData2.DoubleClickActivation = false; 

這個主要的缺點是用戶可能會發現列表視圖一點點鼠標點擊任何過於敏感。

回答

2

.NET專門將此功能添加到ListView。不要問我爲什麼。

要擺脫它,聽NM_DBLCLK反射通知,並在處理程序爲做到這一點::

NativeMethods.NMHDR nmhdr = (NativeMethods.NMHDR)m.GetLParam(typeof(NativeMethods.NMHDR)); 

switch (nmhdr.code) { 
case NM_DBLCLK: 
    // The default behavior of a .NET ListView with checkboxes is to toggle the checkbox on 
    // double-click. That's just silly, if you ask me :) 
    if (this.CheckBoxes) { 
     // How do we make ListView not do that silliness? We could just ignore the message 
     // but the last part of the base code sets up state information, and without that 
     // state, the ListView doesn't trigger MouseDoubleClick events. So we fake a 
     // right button double click event, which sets up the same state, but without 
     // toggling the checkbox. 
     nmhdr.code = NM_RDBLCLK; 
     Marshal.StructureToPtr(nmhdr, m.LParam, false); 
    } 
    break; 

這是衆多問題之一ObjectListView解決了你。即使你不使用整個項目,你也可以查看源代碼並找出如何自己做事。