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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds