Walkthrough: Inherit from a Windows Forms Control with C#

With C#, you can create powerful custom controls through inheritance. Through inheritance you are able to create controls that retain all of the inherent functionality of standard Windows Forms controls but also incorporate custom functionality. In this walkthrough, you will create a simple inherited control called ValueButton. This button will inherit functionality from the standard Windows Forms Button control, and will expose a custom property called ButtonValue.

Create the Project

When you create a new project, you specify its name in order to set the root namespace, assembly name, and project name, and to ensure that the default component will be in the correct namespace.

To create the ValueButtonLib control library and the ValueButton control

  1. In Visual Studio, create a new Windows Forms Control Library project, and name it ValueButtonLib.

    The project name, ValueButtonLib, is also assigned to the root namespace by default. The root namespace is used to qualify the names of components in the assembly. For example, if two assemblies provide components named ValueButton, you can specify your ValueButton component using ValueButtonLib.ValueButton. For more information, see Namespaces.

  2. In Solution Explorer, right-click UserControl1.cs, then choose Rename from the shortcut menu. Change the file name to ValueButton.cs. Click the Yes button when you are asked if you want to rename all references to the code element 'UserControl1'.

  3. In Solution Explorer, right-click ValueButton.cs and select View Code.

  4. Locate the class statement line, public partial class ValueButton, and change the type from which this control inherits from UserControl to Button. This allows your inherited control to inherit all the functionality of the Button control.

  5. In Solution Explorer, open the ValueButton.cs node to display the designer-generated code file, ValueButton.Designer.cs. Open this file in the Code Editor.

  6. Locate the InitializeComponent method and remove the line that assigns the AutoScaleMode property. This property does not exist in the Button control.

  7. From the File menu, choose Save All to save the project.

    Note

    A visual designer is no longer available. Because the Button control does its own painting, you are unable to modify its appearance in the designer. Its visual representation will be exactly the same as that of the class it inherits from (that is, Button) unless modified in the code. You can still add components, which have no UI elements, to the design surface.

Add a Property to Your Inherited Control

One possible use of inherited Windows Forms controls is the creation of controls that are identical in look and feel of standard Windows Forms controls, but expose custom properties. In this section, you will add a property called ButtonValue to your control.

To add the Value property

  1. In Solution Explorer, right-click ValueButton.cs, and then click View Code from the shortcut menu.

  2. Locate the class statement. Immediately after the {, type the following code:

    // Creates the private variable that will store the value of your
    // property.
    private int varValue;
    // Declares the property.
    public int ButtonValue
    {
       // Sets the method for retrieving the value of your property.
       get
       {
          return varValue;
       }
       // Sets the method for setting the value of your property.
       set
       {
          varValue = value;
       }
    }
    

    This code sets the methods by which the ButtonValue property is stored and retrieved. The get statement sets the value returned to the value that is stored in the private variable varValue, and the set statement sets the value of the private variable by use of the value keyword.

  3. From the File menu, choose Save All to save the project.

Test the control

Controls are not stand-alone projects; they must be hosted in a container. In order to test your control, you must provide a test project for it to run in. You must also make your control accessible to the test project by building (compiling) it. In this section, you will build your control and test it in a Windows Form.

To build your control

On the Build menu, click Build Solution. The build should be successful with no compiler errors or warnings.

To create a test project

  1. On the File menu, point to Add and then click New Project to open the Add New Project dialog box.

  2. Select the Windows node, beneath the Visual C# node, and click Windows Forms Application.

  3. In the Name box, enter Test.

  4. In Solution Explorer, right-click the References node for your test project, then select Add Reference from the shortcut menu to display the Add Reference dialog box.

  5. Click the tab labeled Projects. Your ValueButtonLib project will be listed under Project Name. Double-click the project to add the reference to the test project.

  6. In Solution Explorer, right-click Test and select Build.

To add your control to the form

  1. In Solution Explorer, right-click Form1.cs and choose View Designer from the shortcut menu.

  2. In the Toolbox, select ValueButtonLib Components. Double-click ValueButton.

    A ValueButton appears on the form.

  3. Right-click the ValueButton and select Properties from the shortcut menu.

  4. In the Properties window, examine the properties of this control. Note that they are identical to the properties exposed by a standard button, except that there is an additional property, ButtonValue.

  5. Set the ButtonValue property to 5.

  6. In the All Windows Forms tab of the Toolbox, double-click Label to add a Label control to your form.

  7. Relocate the label to the center of the form.

  8. Double-click valueButton1.

    The Code Editor opens to the valueButton1_Click event.

  9. Insert the following line of code.

    label1.Text = valueButton1.ButtonValue.ToString();
    
  10. In Solution Explorer, right-click Test, and choose Set as Startup Project from the shortcut menu.

  11. From the Debug menu, select Start Debugging.

    Form1 appears.

  12. Click valueButton1.

    The numeral '5' is displayed in label1, demonstrating that the ButtonValue property of your inherited control has been passed to label1 through the valueButton1_Click method. Thus your ValueButton control inherits all the functionality of the standard Windows Forms button, but exposes an additional, custom property.

See also