Fire events during design-time?

半世苍凉 提交于 2019-12-10 16:38:18

问题


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.


回答1:


The below should work:

  • Creating a Windows Forms project to host your custom control

  • Creating a control library project

  • 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

more info at http://msdn.microsoft.com/en-us/library/5ytx0z24(v=vs.90).aspx




回答2:


This requires a bit of work - design time interaction is separate from run-time. This may help :

http://www.codeproject.com/Articles/13531/Targeting-Design-Time-Events-of-User-Controls



来源:https://stackoverflow.com/questions/9399260/fire-events-during-design-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!