Custom Design-Time Appearance of Controls

Somtimes, a control needs to appear differently at design time than it does at runtime. This different appearance is often to indicate something to the developer, which will be irrelevant to a runtime user.

The standard approach to achieving this goal is to add additional code in the OnPaint method:

public class MyControl : Control
{
   public override void OnPaint(object sender, PaintEventArgs e)
   {
      //Normal painting here
      if (!DesignMode)
      {
         //Design time painting here
         //Draw a rectangle around the control
      }
   }
}

This works fine, but it is not ideal. Here, we are mixing runtime and design time code. Also, take into account what might happen if another control descends from this class and overrides OnPaint:

public class MyOtherControl : MyControl
{
   public override void OnPaint(object sender, PaintEventArgs e)
   {
      //Call base, which draws a rect around the control
      Base.OnPaint(sender, e);
      //Some code which will draw over the rectangle
   }
}

What we really need is a way of drawing to the e.Graphics object after the OnPaint method has finished completely, and also only during design time.

The solution lies in the ControlDesigner class and the Designer attribute.

public class MyControlDesigner :
       System.Windows.Forms.Design.ControlDesigner
{
   public override void OnPaintAdornments(PaintEventArgs e)
   {
      //Design time painting here
      //Draw a rectangle around the control
   }
}

Then, we just associate MyControlDesigner with the MyControl class. Note how the OnPaint method no longer has any design time painting in it.

[System.ComponentModel.Designer(typeof(MyNameSpace.MyControlDesigner))]
public class MyControl : Control
{
   public override void OnPaint(object sender, PaintEventArgs e)
   {
      //Normal painting here
   }
}


About the Author

Peter Morris

Peter Morris has worked commercially as a computer programmer since 1994, developing software in both Delphi and C#.

He is now self-employed, providing software solutions and consultancy to companies throughout the world, and the author of many programming articles for various magazines.

Peter Morris may be contact via http://www.AirSoftware.co.uk which is his company's official website, or http://www.HowToDoThings.com where you may read numerous programming articles.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds