禁用 TListView 控件中的取消选择
我想使用 TListView (vsIcon) 作为一种选项卡 - 这样只能像选项卡一样选择一个项目。仅选择一项没有问题(禁用 Multiselect 属性)。问题是单击列表视图中图标和文本之间的空白点时取消选择项目。
这是我到目前为止所尝试的:
void __fastcall TForm::ListViewChanging(TObject *Sender, TListItem *Item, TItemChange Change, bool &AllowChange)
{
if (Change == ctState)
{
TPoint CursorRel = ListView->ScreenToClient(Mouse->CursorPos);
AllowChange = (ListView->GetItemAt(CursorRel.x, CursorRel.y) != NULL);
StatusBar->SimpleText = (AllowChange)? "YES" : "NO";
}
}
上面的方法有效,但有一个问题。当鼠标单击空白区域时,它会取消选择该项目,并且键盘上/下箭头不再起作用,尽管该项目看起来仍然被选中。如果我忽略键盘,则对于鼠标选择,它可以正常工作,并且会忽略对空白区域的点击,并在状态栏中显示消息“否”。
任何想法如何解决这个问题,以便它适用于所有可能的选择方法(键盘、鼠标(任何其他?))。
I would like to use TListView (vsIcon) as a kind of tabs - so that only one item can be selected just like tabs. Selecting only one item is no problem (disabling Multiselect property). Problem is deselecting items when clicking on blank spots between icons and text in the listview.
Here is what I tried so far:
void __fastcall TForm::ListViewChanging(TObject *Sender, TListItem *Item, TItemChange Change, bool &AllowChange)
{
if (Change == ctState)
{
TPoint CursorRel = ListView->ScreenToClient(Mouse->CursorPos);
AllowChange = (ListView->GetItemAt(CursorRel.x, CursorRel.y) != NULL);
StatusBar->SimpleText = (AllowChange)? "YES" : "NO";
}
}
The above works but there is a problem. When mouse is clicked on blank area it deselects the item and keyboard up/down arrow no longer work although the item still looks selected. If I ignore keyboard, for mouse selection it works fine and it ignores clicks on blank areas with the message "NO" in the statusbar.
Any ideas how to fix this so it works with all possible selection methods (keyboard, mouse (any other?)).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果点击不在某个项目上,则拦截发送到控件的
WM_LBUTTONDOWN
并停止默认处理。子类化控件,或使用 ApplicationEvents 组件等。带有插入器类的 Delphi 代码示例:Intercept
WM_LBUTTONDOWN
posted to the control and halt default processing if the click is not on an item. Subclass the control, or use an ApplicationEvents component, etc.. Delphi code sample with an interposer class:这是您问题的另一个可能的答案:
Here is another possible answer to your question :