简体   繁体   中英

Fire events during design-time?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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