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

发布于 2024-08-31 00:23:35 字数 128 浏览 4 评论 0原文

我有一个派生自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小姐丶请自重 2024-09-07 00:23:35

这是一个制作可聚焦控件的很好的教程。我只是按照它来确保它有效。此外,还向控件添加了一个按键事件,该事件在控件具有焦点的情况下起作用。

http://en.csharp-online.net/Architecture_and_Design_of_Windows_Forms_Custom_Controls%E2%80% 94Creating_a_Focusable_Control

基本上我所做的就是创建一个继承自 Control 的自定义控件的实例。然后添加了KeyPress、Click 和Paint 事件。按键只是一条消息:

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

单击事件只有:

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

我像这样制作的绘制事件,因此它是可见的:

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

然后,在主窗体中,在创建名为 mycustomcontrol 的实例并添加事件处理程序之后:

mycustomcontrol.Location = new Point(0, 0);
mycustomcontrol.Size = new Size(200, 200);
this.Controls.Add(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:

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.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.

梦初启 2024-09-07 00:23:35

为了可供选择,您的控件必须具有 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 calling SetStyle.

§对你不离不弃 2024-09-07 00:23:35

在没有看到您的代码的情况下,我只能告诉您我创建了一个 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);
    }
}

我将该控件与其他几个按钮一起放在表单上,​​适当地设置制表位,并且行为符合预期。代码中的一些其他默认属性已更改,导致控件无法选择。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文