我正在实现一个继承自 Control
的自定义控件。我希望它是可聚焦的(它是一种列表框)。
在构造函数中,我做
SetStyle(ControlStyles.Selectable, true);
我现在可以使用 Tab 导航到控件。
但是,当控件收到鼠标单击时,它不会自动声明焦点。当然,我可以解决这个问题:
protected override void OnMouseDown(MouseEventArgs e)
{
Focus();
base.OnMouseDown(e);
}
但这感觉就像是不必要的拼凑。这真的是要走的路吗?或者有什么方法可以告诉 Control
在收到鼠标点击时自动声明焦点?
最佳答案
解体救援!事实证明
SetStyle(ControlStyles.UserMouse, true);
成功了。
具有讽刺意味的是,我曾在 documentation 中阅读过:
UserMouse
: If true, the control does its own mouse processing, and mouse events are not handled by the operating system.
这似乎与我想要的完全相反,所以我只尝试将它设置为 false
... 很好,WinForms 文档。
关于c# - 我真的必须在自定义控件的 OnMouseDown 中调用 Focus 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2529246/