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: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

RSS Feeds