This section will walk you through the key steps that are required to create a custom Win Forms control. If you have created controls before, the steps may seem familiar. However, even if you are an experienced control developer, it is recommended that you browse through the steps described here. If you are not familiar with properties, events, and attributes, see Properties, Events, and Custom Metadata Using Attributes before executing the tasks below.
The control that you will develop in the walkthrough, merely allows the alignment of text to be changed.
To create a simple custom control
[C#] public class FirstControl:RichControl{…}
TextAlignment that will be used to format the
Text property inherited from
RichControl.
[C#] private ContentAlignment alignment = ContentlAlignment.Left; public ContentAlignment TextAlignment { get { return alignment; } set { alignment = value; //Invalidate invokes the OnPaint method described //in step 3 below. Invalidate(); } }
TextAlignment defined in step 2 above.
[C#] public class FirstControl : RichControl{ public FirstControl() {…} protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawString(Text, Font, new SolidBrush (ForeColor), ClientRectangle); } }
TextAlignment
property. In a designer such as Visual Studio 7.0, the Category attribute (applied below) will cause the property to be displayed under a logical category. The Description attribute will cause a descriptive string to be displayed at the bottom of the property window when the TextAlignment
property is selected.
[C#] [ Category("Alignment"), Description("Specifies the alignment of text.") ] public ContentAlignment TextAlignment {…}
The code for FirstControl is shown below. The control is created in the namespace CustomWinControls. A namespace provides a logical grouping of related types. You can create your control in a new or existing namespace. In C#, the using
declaration allows types to be accessed from a namespace without using the fully qualified name of the type. For example, with the using
declaration below you can access the class RichControl from System.WinForms simply by typing RichControl instead of System.WinForms. RichControl.
If you compile FirstControl and create an assembly (which will be a .dll file in this case), you will be able to add it to the toolbox of a Win Forms designer such as Visual Studio 7.0. An example that shows how to build and use a custom control is provided in the Win Forms Quickstart.
[C#] namespace CustomWinControls{ using System; using System.ComponentModel; using System.WinForms; using System.Drawing; public class FirstControl:RichControl{ private ContentAlignment alignment = ContentAlignment.Left; [ Category("Alignment"), Description("Specifies the alignment of text.") ] public ContentAlignment TextAlignment { get { return alignment; } set { alignment = value; //Invalidate invokes the OnPaint method Invalidate(); } } //OnPaint aligns text as specified by the //TextAlignment property, by passing a parameter //to the DrawString method of the Grahics object. protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //TextFormat style = (TextFormat)0; StringFormat style = new StringFormat(); style.Alignment = StringAlignment.Near; switch (alignment) { case ContentAlignment.Left: style.Alignment = StringAlignment.Near; break; case ContentAlignment.Right: style.Alignment = StringAlignment.Far; break; case ContentAlignment.Center: style.Alignment = StringAlignment.Center; break; } //Call the DrawString method of the System.Drawing class to write text. //Text and ClientRectangle are properties inherited from //RichControl and Control respectively. e.Graphics.DrawString(Text, Font, new SolidBrush (ForeColor), ClientRectangle, style); } } }