* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
8.5 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
|---|---|---|---|---|---|---|---|---|---|
| Develop a Simple Control | 03/30/2017 |
|
|
86cbe435-45b7-4cb4-9b5a-47418369758d |
How to: Develop a Simple Windows Forms Control
This section walks you through the key steps for authoring a custom Windows Forms control. The simple control developed in this walkthrough allows the alignment of its xref:System.Windows.Forms.Control.Text%2A property to be changed. It does not raise or handle events.
To create a simple custom control
-
Define a class that derives from xref:System.Windows.Forms.Control?displayProperty=nameWithType.
Public Class FirstControl Inherits Control End Classpublic class FirstControl:Control {} -
Define properties. (You are not required to define properties, because a control inherits many properties from the xref:System.Windows.Forms.Control class, but most custom controls generally do define additional properties.) The following code fragment defines a property named
TextAlignmentthatFirstControluses to format the display of the xref:System.Windows.Forms.Control.Text%2A property inherited from xref:System.Windows.Forms.Control. For more information about defining properties, see Properties Overview.[!code-csharpSystem.Windows.Forms.FirstControl#3] [!code-vbSystem.Windows.Forms.FirstControl#3]
When you set a property that changes the visual display of the control, you must invoke the xref:System.Windows.Forms.Control.Invalidate%2A method to redraw the control. xref:System.Windows.Forms.Control.Invalidate%2A is defined in the base class xref:System.Windows.Forms.Control.
-
Override the protected xref:System.Windows.Forms.Control.OnPaint%2A method inherited from xref:System.Windows.Forms.Control to provide rendering logic to your control. If you do not override xref:System.Windows.Forms.Control.OnPaint%2A, your control will not be able to draw itself. In the following code fragment, the xref:System.Windows.Forms.Control.OnPaint%2A method displays the xref:System.Windows.Forms.Control.Text%2A property inherited from xref:System.Windows.Forms.Control with the alignment specified by the
alignmentValuefield.[!code-csharpSystem.Windows.Forms.FirstControl#4] [!code-vbSystem.Windows.Forms.FirstControl#4]
-
Provide attributes for your control. Attributes enable a visual designer to display your control and its properties and events appropriately at design time. The following code fragment applies attributes to the
TextAlignmentproperty. In a designer such as Visual Studio, the xref:System.ComponentModel.CategoryAttribute.Category%2A attribute (shown in the code fragment) causes the property to be displayed under a logical category. The xref:System.ComponentModel.DescriptionAttribute.Description%2A attribute causes a descriptive string to be displayed at the bottom of the Properties window when theTextAlignmentproperty is selected. For more information about attributes, see Design-Time Attributes for Components.[!code-csharpSystem.Windows.Forms.FirstControl#5] [!code-vbSystem.Windows.Forms.FirstControl#5]
-
(optional) Provide resources for your control. You can provide a resource, such as a bitmap, for your control by using a compiler option (
/resfor C#) to package resources with your control. At run time, the resource can be retrieved using the methods of the xref:System.Resources.ResourceManager class. For more information about creating and using resources, see the Resources in Desktop Apps. -
Compile and deploy your control. To compile and deploy
FirstControl,execute the following steps:-
Save the code in the following sample to a source file (such as FirstControl.cs or FirstControl.vb).
-
Compile the source code into an assembly and save it in your application's directory. To accomplish this, execute the following command from the directory that contains the source file.
vbc -t:library -out:[path to your application's directory]/CustomWinControls.dll -r:System.dll -r:System.Windows.Forms.dll -r:System.Drawing.dll FirstControl.vbcsc -t:library -out:[path to your application's directory]/CustomWinControls.dll -r:System.dll -r:System.Windows.Forms.dll -r:System.Drawing.dll FirstControl.csThe
/t:librarycompiler option tells the compiler that the assembly you are creating is a library (and not an executable). The/outoption specifies the path and name of the assembly. The/roption provides the name of the assemblies that are referenced by your code. In this example, you create a private assembly that only your applications can use. Hence, you have to save it in your application's directory. For more information about packaging and deploying a control for distribution, see Deployment.
-
The following sample shows the code for FirstControl. The control is enclosed 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 (in Visual Basic, Imports) allows types to be accessed from a namespace without using the fully qualified name of the type. In the following example, the using declaration allows code to access the class xref:System.Windows.Forms.Control from xref:System.Windows.Forms?displayProperty=nameWithType as simply xref:System.Windows.Forms.Control instead of having to use the fully qualified name xref:System.Windows.Forms.Control?displayProperty=nameWithType.
[!code-csharpSystem.Windows.Forms.FirstControl#1] [!code-vbSystem.Windows.Forms.FirstControl#1]
Using the Custom Control on a Form
The following example shows a simple form that uses FirstControl. It creates three instances of FirstControl, each with a different value for the TextAlignment property.
To compile and run this sample
-
Save the code in the following example to a source file (SimpleForm.cs or SimpleForms.vb).
-
Compile the source code into an executable assembly by executing the following command from the directory that contains the source file.
vbc -r:CustomWinControls.dll -r:System.dll -r:System.Windows.Forms.dll -r:System.Drawing.dll SimpleForm.vbcsc -r:CustomWinControls.dll -r:System.dll -r:System.Windows.Forms.dll -r:System.Drawing.dll SimpleForm.csCustomWinControls.dll is the assembly that contains the class
FirstControl. This assembly must be in the same directory as the source file for the form that accesses it (SimpleForm.cs or SimpleForms.vb). -
Execute SimpleForm.exe using the following command.
SimpleForm
[!code-csharpSystem.Windows.Forms.FirstControl#10] [!code-vbSystem.Windows.Forms.FirstControl#10]