简体   繁体   English

Windows.Forms.Control的具有TabStop和键事件的派生类

[英]Windows.Forms.Control derived class with TabStop and key-events

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. 我有一个从Forms.Control派生的控件,它可以很好地处理鼠标事件和绘画事件,但是按键事件有问题。 I need to handle Left arrow and Right arrow, but so far the Tab control that contains my class eats these. 我需要处理向左箭头和向右箭头,但是到目前为止,包含我的课程的Tab控件都吃掉了这些。

How do I make this control selectable, focusable? 如何使该控件具有选择性,针对性?

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 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. 基本上,我所做的只是创建自定义控件的实例,该控件继承自Control。 Then added KeyPress, Click, and Paint event. 然后添加了KeyPress,Click和Paint事件。 Key press was just a message: 按键只是一个消息:

void CustomControl1_KeyPress(object sender, KeyPressEventArgs e)
{
        MessageBox.Show(string.Format("You pressed {0}", e.KeyChar));
}

Click event just has: 点击事件只有:

this.Focus();
this.Invalidate();

The paint event I made like this, just so it was visible: 我这样进行绘画活动,因此可见:

protected override void OnPaint(PaintEventArgs pe)
{
        if (ContainsFocus)
            pe.Graphics.FillRectangle(Brushes.Azure, this.ClientRectangle);
        else
            pe.Graphics.FillRectangle(Brushes.Red, this.ClientRectangle);
}

Then, in the main form, after making an instance called mycustomcontrol and adding the event handlers: 然后,以主要形式,在创建名为mycustomcontrol的实例并添加事件处理程序之后:

mycustomcontrol.Location = new Point(0, 0);
mycustomcontrol.Size = new Size(200, 200);
this.Controls.Add(mycustomcontrol);

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. 希望对您有所帮助。

In order to be selectable, your control has to have the ControlStyles.Selectable style set. 为了可以选择,您的控件必须具有ControlStyles.Selectable样式集。 You can do this in the constructor by calling SetStyle . 您可以通过调用SetStyle在构造函数中执行此操作。

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: 在看不到您的代码的情况下,我只能告诉您创建了3个标签的容器,并创建了一个非常简单的控件来覆盖OnGotFocus:

public partial class CustomControl1 : Control
{
    public CustomControl1()
    {
        InitializeComponent();
    }

    protected override void OnGotFocus(EventArgs e)
    {
        this.BackColor = Color.Black;
        base.OnGotFocus(e);
    }
    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
    }
}

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. 代码中其他一些默认属性已更改,导致控件无法选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM