Permalink
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (68 sloc) 7.77 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Apply Attributes in Windows Forms Controls
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
controls [Windows Forms], applying attributes
attributes [Windows Forms], applying
Windows Forms controls, applying attributes
af0a3f7f-155b-4ba1-83c4-9cf721331a06
10
dotnet-bot
dotnetcontent
wpickett

How to: Apply Attributes in Windows Forms Controls

To develop components and controls that interact correctly with the design environment and execute correctly at run time, you need to apply attributes correctly to classes and members.

Example

The following code example demonstrates how to use several attributes on a custom control. The control demonstrates a simple logging capability. When the control is bound to a data source, it displays the values sent by the data source in a xref:System.Windows.Forms.DataGridView control. If a value exceeds the value specified by the Threshold property, a ThresholdExceeded event is raised.

The AttributesDemoControl logs values with a LogEntry class. The LogEntry class is a template class, which means it is parameterized on the type that it logs. For example, if the AttributesDemoControl is logging values of type float, each LogEntry instance is declared and used as follows.

[!code-csharpSystem.ComponentModel.AttributesDemoControl#110] [!code-vbSystem.ComponentModel.AttributesDemoControl#110]

[!NOTE] Because LogEntry is parameterized by an arbitrary type, it must use reflection to operate on the parameter type. For the threshold feature to work, the parameter type T must implement the xref:System.IComparable interface.

The form that hosts the AttributesDemoControl queries a performance counter periodically. Each value is packaged in a LogEntry of the appropriate type and added to the form's xref:System.Windows.Forms.BindingSource. The AttributesDemoControl receives the value through its data binding and displays the value in a xref:System.Windows.Forms.DataGridView control.

[!code-csharpSystem.ComponentModel.AttributesDemoControl#1] [!code-vbSystem.ComponentModel.AttributesDemoControl#1]

[!code-csharpSystem.ComponentModel.AttributesDemoControl#100] [!code-vbSystem.ComponentModel.AttributesDemoControl#100]

The first code example is the AttributesDemoControl implementation. The second code example demonstrates a form that uses the AttributesDemoControl.

Class-level Attributes

Some attributes are applied at the class level. The following code example shows the attributes that are commonly applied to a Windows Forms control.

[!code-csharpSystem.ComponentModel.AttributesDemoControl#20] [!code-vbSystem.ComponentModel.AttributesDemoControl#20]

TypeConverter Attribute

xref:System.ComponentModel.TypeConverterAttribute is another commonly used class-level attribute. The following code example shows its use for the LogEntry class. This example also shows an implementation of a xref:System.ComponentModel.TypeConverter for the LogEntry type, called LogEntryTypeConverter.

[!code-csharpSystem.ComponentModel.AttributesDemoControl#5] [!code-vbSystem.ComponentModel.AttributesDemoControl#5]

Member-level Attributes

Some attributes are applied at the member level. The following code examples show some attributes that are commonly applied to properties of Windows Forms controls.

[!code-csharpSystem.ComponentModel.AttributesDemoControl#21] [!code-vbSystem.ComponentModel.AttributesDemoControl#21]

AmbientValue Attribute

The following example demonstrates the xref:System.ComponentModel.AmbientValueAttribute and shows code that supports its interaction with the design environment. This interaction is called ambience.

[!code-csharpSystem.ComponentModel.AttributesDemoControl#23] [!code-vbSystem.ComponentModel.AttributesDemoControl#23]

Databinding Attributes

The following examples demonstrate an implementation of complex data binding. The class-level xref:System.ComponentModel.ComplexBindingPropertiesAttribute, shown previously, specifies the DataSource and DataMember properties to use for data binding. The xref:System.ComponentModel.AttributeProviderAttribute specifies the type to which the DataSource property will bind.

[!code-csharpSystem.ComponentModel.AttributesDemoControl#25] [!code-vbSystem.ComponentModel.AttributesDemoControl#25]

[!code-csharpSystem.ComponentModel.AttributesDemoControl#26] [!code-vbSystem.ComponentModel.AttributesDemoControl#26]

Compiling the Code

  • The form that hosts the AttributesDemoControl requires a reference to the AttributesDemoControl assembly in order to build.

See Also

xref:System.IComparable
xref:System.Windows.Forms.DataGridView
Developing Custom Windows Forms Controls with the .NET Framework
Attributes in Windows Forms Controls
How to: Serialize Collections of Standard Types with the DesignerSerializationVisibilityAttribute