Prevent ItemChecked event on a ListView from interfering with SubItemClicked using C#

被刻印的时光 ゝ 提交于 2019-12-13 01:56:12

问题


I am using a in-place editable listview control for a project.

The editable listview adds a 'SubItemClicked' event so that each 'cell' can be edited.

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

I also have the listview checkboxes enabled with a 'ItemChecked' event.

The problem is that once the 'ItemChecked' event is enabled double-clicking on any row fires the 'ItemChecked' event and prevents the 'SubItemClicked' event from firing.

Is there a way to enforce the need to actually 'check' the listview checkbox instead of firing whenever the row is double-clicked?

One possible solution is to disable the listview's 'DoubleClickActivation':

this.lstShuntData2.DoubleClickActivation = false;

The main drawback to this is that the users may find the listview a little too sensitive to any mouseclick.


回答1:


.NET specifically adds this functionality to a ListView. Don't ask me why.

To get rid of it, listen for NM_DBLCLK reflected notification, and in the handler for that do this::

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;

This is one of the many problems that ObjectListView solves for you. Even if you don't use the whole project, you can look at the source code and figure out how to do things yourself.



来源:https://stackoverflow.com/questions/3065183/prevent-itemchecked-event-on-a-listview-from-interfering-with-subitemclicked-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!