【问题标题】:Prevent ItemChecked event on a ListView from interfering with SubItemClicked using C#使用 C# 防止 ListView 上的 ItemChecked 事件干扰 SubItemClicked
【发布时间】:2011-03-05 03:47:21
【问题描述】:

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

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

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

我还通过“ItemChecked”事件启用了列表视图复选框。

问题在于,一旦启用“ItemChecked”事件,双击任何行都会触发“ItemChecked”事件并阻止“SubItemClicked”事件触发。

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

一种可能的解决方案是禁用列表视图的“DoubleClickActivation”:

this.lstShuntData2.DoubleClickActivation = false;

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-22
      • 2019-05-26
      • 2011-06-23
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      相关资源
      最近更新 更多