Saturday, March 24, 2007

Give your Custom Controls a Design-Time Support part1

Built in controls come with design-time support such as smart tags, snaplines ,editors, type convertors, and more.

In this series I'll try to show how to give your custom control the design-time support to make rich and easy to use controls and components.

In this first part I'll show the Design Time Architecture and define some terms.


And now after the great picture lets get to know what are these terms :

  • Type , member1 stand for the Control class and the members(Events,Properties, methods..) of the control.
  • Designers, TypeConverters and UI Type Editors are main three services given to Design Time Environment,we will discuss each one in bigger scope later.
  • Attributes connect the control and members with the services which in turn are used in the Design time environment.
So how to add design-time support to controls?

The key answer of this question is Attributes :
Attributes are applied to types and type members to associate them with these design-time support providers.


Attributes associate a type or type member with a class that extends design-time behavior.
I'll start with the easy Attributes:

1- DescriptionAttribute : Specifies a description for a property or event, that appears at the bottom of the property grid.

2-CategoryAttribute: Specifies the name of the category in which to group the property or event when displayed in a PropertyGrid control set to Categorized mode.

example:
[Category("The category")]

[Description("This is the description of MyProperty")]
public int MyProperty
{
...
}

3-DefaultValueAttribute : specifies the default value of a property, it is created with any value. A member's default value is typically its initial value. A visual designer can use the default value to reset the member's value. Code generators can use the default values also to determine whether code should be generated for the member.

[DefaultValue(10)]
public int MyProperty { ... }

4-BrowsableAttribute :Specifies whether a property or event should be displayed in a Properties window.
[Browsable(true)]
public int MyProperty { ... }

Of course we are giving design time support we don't want to make our property or event invisible, however in complex controls thier are properties that are not allowed to be rendered in design time {e.g. Region, Client Rectangle and Client Size.} so this attribute might become handy one day.

5-EditorBrowsableAttribute : Marks the property to be not shown or shown in the code editor(to appear when coding in intellisene)
[EditorBrowsable(EditorBrowsableState.Never)]
public int MyProperty { ... }

To Be Continued...

2 comments:

Ahmed IG said...

great Start, waiting for the next steps to build a truly professional Control similar to the Progress Disk ;)
Thanks

Amr Elsehemy said...

Thank you ,
I am sure after understanding this series and the next one you will be able to build controls even more better than the ProgressDisk hundered times.