C# Programming > Windows Forms

User Controls
Custom Properties


Custom Controls

C#.NET comes with a powerful base to create custom user controls. Let's explore a simple process to make your custom controls more similar to default .NET controls and thus more professional.

If you add a property to your custom control you will notice in the designer it's shown under the category "Misc". While that is okay, sometimes you want to fine tune how the designer will treat the custom property.

The Format

We're going to go over the basic fine tunings of any given property of a custom user control. But first let's establish the format:

[properties go here]
public int MyProperty

In this case the brackets [ ] are included in the source code. So let's see the kinds of things you can do with this.

Customizing the Designer

To customize how the property is displayed by the designer you need four basic attributes: Description, Category, DefaultValue, Browsable.

Description - This is the description that the programmer sees at the very bottom of the Properties Window.

Category - This is the section the property is listed under if the Properties Window is Categorized. You can even specify an existing category, like "Appearance" for example to insert your custom property with other common .Net properties. Otherwise you can use this attribute to create property category.

DefaultValue - Specifies the default value of the property when the user control is first created.

Browsable - If set to false, the property won't be displayed at all in the designer. This attribute is useful if your property is to be modified only programmatically.

So for example:

[Description("Sets the Angle"),
public int Angle

Set the Default Event

If you add a Button to your Windows Form, and you double click the designer, do you notice how a Click event is automatically added? Now try it again with a custom control. The default event create will be a Load event. Not very professional. Telling the designer which event to use is actually quite simple, the trick is the attribute has to be specified to the entire class:

public partial class AngleSelector : UserControl

Now if you double-click the user control the default event created will be the one specified.

IntelliSense Integration

The final step to formalize a property is to add a description for IntelliSense to display. This format is slightly different:

/// <summary>
/// Gets or Sets the Angle
/// </summary>

Anything inside the summary tags will be the description displayed by IntelliSense. Simply add the code segment before the [ ] attributes. Note that these tags are the beginning of XML Documentation built-in to C#.NET.

Back to C# Article List