2

If I have user control, in which I override the OnPaint() to display custom graphics. Why does this not show when viewing the control in the design mode of visual studio?

The user control will appear empty when viewed in design mode. However if the control is added to a form, which is viewed in design mode it will appear black.

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.Clear(Color.Black);

        base.OnPaint(e);
    }
}

Is there anything I can do to make this show up when viewing the control in design mode?

  • 1
    Not sure what to do but it is the same as with a Form: Its Paint event also won't be used by the designer.. [This may help..](http://www.codeproject.com/Articles/13531/Targeting-Design-Time-Events-of-User-Controls) – TaW Feb 09 '16 at 20:15
  • 3
    At design-time, the designer creates the base class of the class that you are designing. UserControl.OnPaintBackground() draws the BackColor and UserControl.OnPaint() draws nothing. Once you drop the UserControl on a form it is no longer in design mode and you get to see what your OnPaint() method. You could derive another user control from this one and you'll see black at design-time. – Hans Passant Feb 09 '16 at 20:21
  • Wow, that is good to know and a rather simple way to 'solve' the 'issue'.. – TaW Feb 09 '16 at 21:58

4 Answers4

0

If you build your project the control should appear in the toolbox of the form designer. If you drag it to the form it does appear Black as intended.

As far as i know it is not possible to have this at Designtime of the control itself.

Marco Rebsamen
  • 605
  • 7
  • 30
0

Custom user controls are not displayed on the designer if you have "mocking mode" turned on. Depending on your version of Visual Studio, there will either be a button beside the designer or an option in Tools>Options and you will want to "Enable Project Code".

Harriet
  • 97
  • 3
0

This is intended behavior, as you describe it.

Your inherited control must be built first in order to execute the overridden OnPaint() method.

Once built it will appear in the Designer toolbox, where it can be added to a form. The form in the designer hosts the built control, which executes OnPaint(), hence why the black background is visible even though you are still in design mode.

A quick way to illustrate this is to change Color.Black to another color and see what happens. It will not update in the Designer until you rebuild the solution!

Lemonseed
  • 1,644
  • 1
  • 15
  • 29
0

I had the same issue and I really think it would be nice to be able to just look at your control during development without having to add it to another form.

And I think I have a solution! It's neat, but I have not worked with it more then a couple of minutes and there might be some drawbacks so let me know! :)

Just add a Mockup class on top of your UserControl-code like this:

internal class MyUserControlDesigner : MyUserControl
{

}

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.Clear(Color.Black);

        base.OnPaint(e);
    }
}

By making it internal it will not pop up in the toolbox and if you switch to "View Designer" it will show the first class in the file! Soo.. it works well for me!

If you resize it or change any properties in the Designer this will update MyUserControlDesigner class with InitializeComponent code that only will be run in the designer which is perfect for the purpose of testing your Control!

Hope this was what you where looking for more then 4 years ago! :D

edit: You need to build the project for new behaviour to show in designer!