Click here to Skip to main content
15,068,988 members
Articles / Desktop Programming / Windows Forms
Posted 22 Feb 2005


134 bookmarked

Creating Custom Controls-Providing Design Time Support 1

Rate me:
Please Sign up or sign in to vote.
4.49/5 (58 votes)
22 Feb 20056 min read
This article explains how to develop customized design time extensions.


During my interactions in the C# Forum, I came across questions on providing Design time support for custom controls. So I thought of sharing a little experience I have on this.

The Design time support architecture allows us to develop customized design time extensions using which we can configure our controls/properties of the control. Ex: Dock, Anchor properties of Button and other controls. I also encountered a similar scenario while developing a custom user control, where I needed to provide User Interface for modifying its property at design time.

Let's see in brief the .NET FrameWork support for this.

.NET Support

The .NET FrameWork has interfaces that we can use to implement customized design time support. There are three ways in which we can provide this.

  • UITypeEditor
  • TypeConverters
  • Designers


UITypeEditors allow us to provide a custom user interface for editing the value of a property of the control. This also allows us to display the visual representation of the current value in the properties window.


TypeConverters allow us to convert the values between the types. They also allows us to provide logic for configuration of a property in the property browser.


Designers allow us to customize the behaviour of our control at design time.

This article explains how we can use UITypeEditors and TypeConverters, to provide design time support. Using Designers are explained in the next article.

Using UITypeEditor/Property Editor

Dock, Anchor properties of .NET controls provide user interface for supplying/setting their value. These are called as Property Editors. We can use .NET FrameWork UITypeEditor class to create such interfaces for our custom properties. Property Editors are generally displayed in the drop-down area inside the properties window itself. But we can also make them to appear as modal windows.

Let's see how we can create Property Editors for our custom properties. We will use a custom control (Rectangle Control) for explaining this. Rectangle Control draw a filled rectangle. This control exposes two custom properties, RectWidth and RectHeight. Users can use these properties for setting width and height of the rectangle.

It will be more useful if we can provide a user interface (TrackBar) for supplying these values instead of asking the users to enter the values.

Sample screenshot

Fig 1: Property Editor for Setting RectHeight and RectWidth

The first step in creating the property editor is to specify the editor (WidthEditor) in the Editor attribute associated with the property procedure.

[Description("Width of the rectangle"),Editor(typeof(WidthEditor),
public int RectWidth
        return this.width;
        if( value > 0){
            this.width = value;

The Editor attribute takes two System.Type arguments. The first argument is the type of editor. (WidthEditor - we will be creating this in the second step). The second argument is always typeof(UITypeEditor).

In the second step we have to create WidthEditor class. This class should be inherited from System.Drawing.Design.UITypeEditor class and must override two methods. They are GetEditStyle() and EditValue() methods.

The form designer calls GetEditStyle() method when it's filling the properties window with the values of all the properties of the control. This method returns an enumerated value that tells the designer whether the property editor is going to display in the drop-down area or modal form. In our example the property editor is displayed in the drop-down area.

public override UITypeEditorEditStyle 

The form designer calls the EditValue() method when the user clicks the button beside the property name. The EditValue() method creates TrackBar control and sets the configuration details for it. (Like Range, Orientation). This method uses IWindowsFormEditorService class object to show the TrackBar control in the drop-down area.

IWindowsFormsEditorService frmsvr = 
frmsvr.DropDownControl(tbr); //tbr = Object of Type track 

That's all we need to do. Now we can use this control by adding it to the from designer. (The attached solution has TestCustomControl project which consumes this control). When we select RectWidth or RectHeight properties, a TrackBar control will be displayed. The values of these properties can be changed using the TrackBar. When we change these values, the new value will appear in the property browser and rectangle will be redrawn with the current dimension . Very simple. Am I right?

If we want to display the modal form instead of showing it in drop-down area, we need to follow the same steps with only two differences.

  1. Return value of GetEditStyle()should be UITypeEditorEditStyle.Modal.
  2. We need to create a Form class and should add all the controls that make up the editor interface to this Form. In the EditValue() method we need to create an instance of this form. Now to show this form we need to call ShowModal() method of IWindowsFormsEditorService class.

Let's move into the second topic of this article. Using TypeConverters.

Using TypeConverters

We know that few properties (Font, Size) return objects instead of scalar values and these properties are displayed in the property browser with a "+" sign. If our custom control contain any properties that return .NET FrameWork defined objects (Font, Point, Location, Size) then they will inherit the similar behaviour. But if our custom control contains any property that returns a custom object, then how can we achieve the similar behaviour? To get that, we need to create a custom TypeConverter class.

Fig2: PhoneData property returning a custom object

Fig2:PhoneData property with "+" sign

The current example use PhoneNumber custom control. This control allows the users to enter Name, CountryCode, AreaCode, PhoneNumber details. Let's expose this data in a single property instead of exposing them in five different properties.

The first step in doing this is to create PhoneNumber class that wrap all the properties and raise PropertyChanged event when any property changes. We need to decorate this class with TypeConverter attribute.

public class PhoneNumber

TypeConverter attribute tells the form designer that PhoneTypeConverter is associated with PhoneNumber class.

In the second step we need to create PhoneTypeConverter class which is derived from System.ComponentModel.TypeConverter and override two methods. They are GetPropertiesSupported(), GetProperties() methods.

GetPropertiesSupported() method returns whether the object supports properties or not. Form designer use this value to display "+" symbol at PhoneData property. Since our object supports properties GetPropertiesSupported() method returns true value.

GetProperties() method returns PropertiesDescriptorCollection object which will describe the items that will appear when the "+" symbol is clicked.

public override PropertyDescriptorCollection 
GetProperties(ITypeDescriptorContext context, 
               object value, Attribute[] attributes)

The third and last step is to set the DesignerSerializationVisibility attribute of the PhoneData to Content.


DesignerSerializationVisibility attribute specify the type of persistence to use while serializing a property on a control at design time. Setting this value to content tells VS.NET that, it should serialize the contents of this PhoneData property.

Now it's the time for compiling and using the control. When we add this control to the form designer the PhoneData property of the PhoneNumber control appear with "+" sign in the properties window. Clicking on the "+" sign will expand all the items. When we modify any value it will be displayed in the PhoneNumber control.


In this article we saw how we can use PropertyEditor and TypeConverters for enhancing design time support of our custom controls. In the next article we will see how we can use designers.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Kodanda Pani
Web Developer
India India
Working on .NET for last 6 years. Currently working for TCS.

Comments and Discussions

QuestionQuestion Please Pin
Eng.Yahya922-Jul-15 20:39
MemberEng.Yahya922-Jul-15 20:39 
GeneralMy vote of 5 Pin
Jibsta-Man23-May-13 0:16
MemberJibsta-Man23-May-13 0:16 
QuestionConfiguration Pin
Pedrazzi6-Aug-12 9:41
MemberPedrazzi6-Aug-12 9:41 
QuestionNeed more help! Pin
readywork18-Aug-11 11:43
Memberreadywork18-Aug-11 11:43 
GeneralMy vote of 5 Pin
ODHeart9-Mar-11 1:53
MemberODHeart9-Mar-11 1:53 
GeneralRe: My vote of 5 Pin
Nick Alexeev26-May-11 18:03
professionalNick Alexeev26-May-11 18:03 
QuestionHow to make custom property editor work in VBA? Pin
ODHeart9-Mar-11 1:20
MemberODHeart9-Mar-11 1:20 
GeneralAwesome - Definately a 5 Stars Pin
GreatBigYetti25-Feb-11 6:30
MemberGreatBigYetti25-Feb-11 6:30 
Generalinherited controls design view Pin
Member 348723110-Aug-10 23:52
MemberMember 348723110-Aug-10 23:52 
GeneralMy vote of 5 Pin
Hua Yujun4-Aug-10 23:11
MemberHua Yujun4-Aug-10 23:11 
GeneralMy vote of 5 Pin
vla_dd2-Jul-10 5:33
Membervla_dd2-Jul-10 5:33 
GeneralEvents Pin
booies6-Jul-07 10:47
Memberbooies6-Jul-07 10:47 
GeneralExpandableObjectConverter Pin
nseveno24-May-07 21:42
Membernseveno24-May-07 21:42 
GeneralSee Also These cool articles! Pin
S.Serpooshan18-Jan-07 6:11
MemberS.Serpooshan18-Jan-07 6:11 
QuestionSet of controls Pin
the Same [aaaaaa]1-Dec-06 7:47
Memberthe Same [aaaaaa]1-Dec-06 7:47 
GeneralCustom datagrid Pin
hemaral3-Oct-06 10:04
Memberhemaral3-Oct-06 10:04 
QuestionA specific question Pin
nilsgate8-Sep-06 3:14
Membernilsgate8-Sep-06 3:14 
GeneralCancelling the drop down Slider(Trackbar) Pin
Jaswinder Singh Kohli2-Jul-05 12:20
sussJaswinder Singh Kohli2-Jul-05 12:20 
GeneralRe: Cancelling the drop down Slider(Trackbar) Pin
S.Serpooshan18-Jan-07 6:15
MemberS.Serpooshan18-Jan-07 6:15 
GeneralHelp!!! Pin
Tim McCurdy20-Mar-05 23:27
MemberTim McCurdy20-Mar-05 23:27 
GeneralRe: Help!!! Pin
Kodanda Pani21-Mar-05 19:57
MemberKodanda Pani21-Mar-05 19:57 
GeneralRe: Help!!! Pin
Tim McCurdy26-Mar-05 6:29
MemberTim McCurdy26-Mar-05 6:29 
GeneralRe: Help!!! Pin
Almighty Bob28-Mar-05 18:37
MemberAlmighty Bob28-Mar-05 18:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.