带有 TabStop 和按键事件的 Windows.Forms.Control 派生类
我有一个派生自 Forms.Control 的控件,它可以很好地处理鼠标事件和绘画事件,但我在处理按键事件时遇到问题。我需要处理向左箭头和向右箭头,但到目前为止,包含我的类的选项卡控件吃掉了这些。
如何使该控件可选择、可聚焦?
I've got a control that derives from Forms.Control, it handles mouse events and paint events just fine, but I'm having a problem with key events. I need to handle Left arrow and Right arrow, but so far the Tab control that contains my class eats these.
How do I make this control selectable, focusable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个制作可聚焦控件的很好的教程。我只是按照它来确保它有效。此外,还向控件添加了一个按键事件,该事件在控件具有焦点的情况下起作用。
http://en.csharp-online.net/Architecture_and_Design_of_Windows_Forms_Custom_Controls%E2%80% 94Creating_a_Focusable_Control
基本上我所做的就是创建一个继承自 Control 的自定义控件的实例。然后添加了KeyPress、Click 和Paint 事件。按键只是一条消息:
单击事件只有:
我像这样制作的绘制事件,因此它是可见的:
然后,在主窗体中,在创建名为 mycustomcontrol 的实例并添加事件处理程序之后:
该示例更加简洁比我的五分钟代码,只是想确保可以通过这种方式解决您的问题。
希望这有帮助。
Here is a nice tutorial to make a focusable control. I just followed it to make sure it works. Also, added a keypress event to the control which works on condition that the control has focus.
http://en.csharp-online.net/Architecture_and_Design_of_Windows_Forms_Custom_Controls%E2%80%94Creating_a_Focusable_Control
Basically all I did was make an instance of my custom control, which inherits from Control. Then added KeyPress, Click, and Paint event. Key press was just a message:
Click event just has:
The paint event I made like this, just so it was visible:
Then, in the main form, after making an instance called mycustomcontrol and adding the event handlers:
The example is much neater than my five minute code, just wanted to be sure that it was possible to solve your problem in this way.
Hope that was helpful.
为了可供选择,您的控件必须具有
ControlStyles.Selectable
样式集。您可以通过调用 < 在构造函数中执行此操作代码>SetStyle。In order to be selectable, your control has to have the
ControlStyles.Selectable
style set. You can do this in the constructor by callingSetStyle
.在没有看到您的代码的情况下,我只能告诉您我创建了一个 3 选项卡容器,并创建了一个非常简单的控件来覆盖 OnGotFocus:
我将该控件与其他几个按钮一起放在表单上,适当地设置制表位,并且行为符合预期。代码中的一些其他默认属性已更改,导致控件无法选择。
Without seeing your code, I can only tell you that I created a 3 tab container, and created a very simple control that overrode the OnGotFocus:
I dropped the control on the form along with a couple of other buttons, set the tab stops appropriately, and the behavior was as expected. Some other default property has been changed in code that is causing the control to not be selectable.