问题描述
我正在为项目使用就地可编辑listview 控件
可编辑的列表视图添加了一个"SubItemClicked"事件,以便可以编辑每个单元格".
The editable listview adds a 'SubItemClicked' event so that each 'cell' can be edited.
lstSD2.SubItemClicked += new ListViewEx.SubItemEventHandler(lstSD2_SubItemClicked);
我还启用了带有"ItemChecked"事件的列表视图复选框.
I also have the listview checkboxes enabled with a 'ItemChecked' event.
问题在于,一旦启用了"ItemChecked"事件,双击任何行都会触发"ItemChecked"事件,并阻止触发"SubItemClicked"事件.
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?
一种可能的解决方案是禁用列表视图的"DoubleClickActivation":
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.
推荐答案
.NET专门将此功能添加到ListView.不要问我为什么.
.NET specifically adds this functionality to a ListView. Don't ask me why.
要摆脱它,请监听NM_DBLCLK
反映的通知,并在处理程序中执行以下操作:
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;
这是 ObjectListView 为您解决的众多问题之一.即使您没有使用整个项目,也可以查看源代码并弄清楚如何自己做事.
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.
这篇关于防止ListView上的ItemChecked事件干扰使用C#的SubItemClicked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!