Design-Time Attributes for Components

1 篇文章 / 0 new
Design-Time Attributes for Components

If you are not familiar with applying attributes to provide metadata to the common language runtime, see Extending Metadata Using Attributes. Because components can be displayed in a designer, such as Visual Studio, they require attributes that provide metadata to design-time tools. This section describes and provides a list of commonly used design-time attributes.

Attributes and Designer Support

Design-time attributes are essential for displaying your control and its members correctly at design time, as they provide valuable information to a visual design tool.

In the following code fragment, the CategoryAttribute attribute enables the property browser to display the TextAlignment property in the Alignment category. The DescriptionAttribute attribute allows the property browser to provide a brief description of the property when a user clicks on it.


Description("Specifies the alignment of text.")
public ContentAlignment TextAlignment { //... }

In Visual C# and Visual Basic, an attribute class named AttributeNameAttribute can be referenced simply as AttributeName in the attribute syntax.

Some design-time attributes are applied at the class level. A DesignerAttribute attribute is applied at the class level and informs the forms designer which designer class to use to display the control. Components are associated with a default designer (System.ComponentModel.Design.ComponentDesigner), and Windows Forms and ASP.NET server controls are associated with their own default designers. Apply DesignerAttribute only if you define a custom designer for your component or control.

// Associates the designer class SimpleControl.Design.SimpleDesigner
// with Simple.
[ Designer(typeof(SimpleControl.Design.SimpleDesigner))]
    public class Simple : WebControl { //... }
Common Attributes for Properties and Events

The following table lists the attributes that are commonly applied to properties and events.



Applied To



Properties and events

Specifies whether a property or an event should be displayed in the property browser.


Properties and events

Specifies the name of the category in which to group a property or event. When categories are used, component properties and events can be displayed in logical groupings in the property browser.


Properties and events

Defines a small block of text to be displayed at the bottom of the property browser when the user selects a property or event.



Specifies whether a property is interesting to bind to.



(Insert this attribute before the class declaration.)

Specifies the default property for the component. This property is selected in the property browser when a user clicks on the control.



Sets a simple default value for a property.



Specifies the editor to use for editing (changing) a property in a visual designer.



Specifies that a property can be localized. Any properties that have this attribute are automatically persisted into the resources file when a user chooses to localize a form.



Specifies whether (and how) a property displayed in the property browser should be persisted into code.



Specifies the type converter to use for converting the type of the property to another data type.



(Insert this attribute before the class declaration.)

Specifies the default event for the component. This is the event that is selected in the property browser when a user clicks on the component.

Unless otherwise stated, attributes for properties and events are placed in code immediately before the property or event declaration, as shown in the following example.

// To apply CategoryAttribute to the BorderColor 
// property, place it immediately before the declaration
// of the BorderColor property.
public Color BorderColor;

// To apply DescriptionAttribute to the Click event, 
// place it immediately before the declaration
// of the Click event.
[Description("The Click event of the button")]
public event EventHandler Click;

For information about design-time attributes that associate designers with components and controls, see Extending Design-Time Support.

In addition to using the attribute classes defined in the .NET Framework class library, you can define your own attribute classes. For details, refer to the documentation for your programming language or see Writing Custom Attributes.

Free Web Hosting