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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds