代码之家  ›  专栏  ›  技术社区  ›  John M

防止ListView上的ItemChecked事件干扰使用C单击的子项#

  •  1
  • John M  · 技术社区  · 15 年前

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

    可编辑的ListView添加了一个“SubitemClicked”事件,以便可以编辑每个“单元格”。

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

    我还使用“itemchecked”事件启用了ListView复选框。

    问题是,一旦启用了“itemchecked”事件,双击任何行将激发“itemchecked”事件,并阻止激发“subitemchecked”事件。

    是否有一种方法可以强制实际“检查”ListView复选框而不是在双击行时激发?

    一种可能的解决方案是禁用ListView的“DoubleClickActivation”:

    this.lstShuntData2.DoubleClickActivation = false;
    

    这样做的主要缺点是,用户可能会发现ListView对任何鼠标点击都有点过于敏感。

    1 回复  |  直到 9 年前
        1
  •  3
  •   Grammarian    15 年前

    .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 为你解决。即使您不使用整个项目,您也可以查看源代码并自行解决问题。