阻止ListView上的ItemChecked事件使用C#干扰SubItemClicked

时间:2010-06-17 20:00:28

标签: c# winforms .net-3.5 listview checkbox

我正在为项目使用in-place editable listview控件。

可编辑列表视图添加了一个“SubItemClicked”事件,以便可以编辑每个“单元格”。

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

我还使用'ItemChecked'事件启用了列表视图复选框。

问题是,一旦启用'ItemChecked'事件,双击任何一行就会触发'ItemChecked'事件并阻止'SubItemClicked'事件被触发。

有没有办法强制实际需要“检查”列表视图复选框,而不是在双击行时触发?

一种可能的解决方案是禁用listview的'DoubleClickActivation':

this.lstShuntData2.DoubleClickActivation = false;

这样做的主要缺点是用户可能会发现列表视图对鼠标点击过于敏感。

1 个答案:

答案 0 :(得分:3)

.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为您解决的众多问题之一。即使您不使用整个项目,也可以查看源代码并找出如何自己做事。