在设计时发生火灾事件? [英] Fire events during design-time?

查看:90
本文介绍了在设计时发生火灾事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在设计一个自定义制表符控件类,该类继承自System.Windows.Forms.Control。
问题是在设计时不会触发任何事件(至少没有测试过,包括鼠标和键盘事件)。
这对我来说是个问题,因为无法在设计器中的选项卡页面之间切换对用户来说非常不便。我一直在做一些研究,但似乎无法实现。这使我感到奇怪,因为.NET框架附带的许多控件都支持设计时交互。以TabControl为例。您可以在设计时在其页面之间切换。

I'm currently designing a custom tab control class which derives from System.Windows.Forms.Control. The problem is that no (at least none of the ones I have tested, which include mouse as well as keyboard events) events get fired during design-time. This is a problem to me as being unable to switch between tab pages in the designer is quite inconvenient to the user. I've been doing some research and it seems that what I am trying to accomplish is not possible. This has made me wonder, as a lot of controls that come with the .NET framework support design-time interaction. Take the TabControl as an example. You can switch between its pages just fine whilst designing.

所以我的问题是:有没有一种方法可以使鼠标和键盘事件在设计时正常工作?

So my question is: Is there a way to get mouse and keyboard events working in design-time?

此外,很抱歉,我没有提供代码段。但是我认为这不是真的必要。对于那些想尝试一下的人,这里是我快速创建的简单按钮类:

Also, sorry that I haven't provided a code snippet. But I don't think it is really necessary. For those of you who want to try it out, here is a simple button class I've quickly created:

public class MyButton : Control
{

    private bool hover = false;

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Color color = hover ? Color.DarkBlue : Color.Blue;
        e.Graphics.FillRectangle(new SolidBrush(color), DisplayRectangle);
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(DisplayRectangle.Location, new Size(DisplayRectangle.Width - 1, DisplayRectangle.Height - 1)));
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);

        hover = true;
        Refresh();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);

        hover = false;
        Refresh();
    }

}

您将看到按钮的颜色在设计期间没有变化。

You will see that the button's color is not changing during design time.

推荐答案

下面的方法应该起作用:

The below should work:


  • 创建一个Windows Forms项目来承载您的自定义控件

  • Creating a Windows Forms project to host your custom control

创建一个控件库项目

将属性添加到自定义控件

Adding a property to your custom control

将自定义控件添加到主机窗体

Adding your custom control to the host form

设置项目以进行设计时调试

Setting up the project for design-time debugging

在设计时调试自定义控件

Debugging your custom control at design time

更多信息,请参见 http://msdn.microsoft.com/zh-CN/library/5ytx0z24(v = vs.90).aspx

这篇关于在设计时发生火灾事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆