Partager via


Custom Control Development

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The .NET Compact Framework provides Windows Forms controls that can meet the needs for most device projects. These controls are designed to use constrained resources efficiently and, therefore, do not provide support for all inherited properties, methods, and events. To satisfy missing functionality, you can derive your own custom controls from common controls. A custom control can be created by defining a public type that inherits from the Control class or from an existing UserControl in your assembly.

The simplest control customization is overriding a method on a common control. For example, you can override the inherited OnKeyPress method for a TextBox control to provide code that restricts input to only numeric characters.

Note

The .NET Compact Framework does not support all properties, methods, and events on Windows Forms controls that are inherited from their base controls.

You can derive from common controls to:

  • Override your own properties, methods, and events on common controls.

  • Define additional properties, methods, and events for a control.

  • Build a compound control, such as a collection of TextBox and Button controls.

  • Define how a control responds to user actions, such as a TextBox that accepts only numeric data.

The .NET Compact Framework does not currently provide the capability to add a custom control for design-time access.

If you want to do more elaborate customization, you must create a custom control derived from the Control class. You can draw the control by overriding the OnPaint method and create non-UI controls for such needs as a custom event.

The base Control class does not know how a derived control needs to be drawn and does not provide any painting logic in the OnPaint method. The OnPaint method of Control just dispatches the Paint event to registered event receivers.

The .NET Compact Framework rises the Paint and Resize events for only the Form and Control classes.

Adding Custom Controls to the Toolbox

When you create a project for a custom control in Microsoft Visual Studio 2005, the control is automatically added to the Toolbox when the application is compiled. You can create a custom control using one of the following project types:

  • Control Library

  • Class Library

  • Class Library (1.0)

To add a custom control to the Toolbox, click Choose Toolbox Items from the Tools menu. You can then browse for the control's assembly.

Custom Control Techniques and Considerations

Please note the following when creating custom controls:

  • The .NET Compact Framework does not support inheriting values from a parent control, as is possible with some controls in the full NET Framework. To work around this issue, you can use the OnParentChanged method to determine when the parent changes and take the appropriate action. The following code example shows changing the background color when the parent changes:

    protected override void OnParentChanged(EventArgs e)
    {
        base.OnParentChanged(e);
        this.BackColor = Parent.BackColor;
    }
    
    Protected Overrides Sub OnParentChanged(ByVal e As EventArgs) 
        MyBase.OnParentChanged(e)
        Me.BackColor = Parent.BackColor
    End Sub
    
  • The .NET Compact Framework does not support inheriting the font of a parent control.

  • To detect the location on a custom control where a "tap-and-hold" event occurred to display a shortcut menu, handle the Popup event and then query the current mouse coordinates using the MousePosition property.

  • To tab out of the custom control to the previous control, use the following code in the KeyDown event handler when an Up key is detected.

    this.Parent.Controls(
        this.Parent.GetChildIndex(customcontrol) - 1).Focus()
    
    Me.Parent.Controls( _
        Me.Parent.GetChildIndex(customcontrol) - 1).Focus
    

How-To Topics

The following How-to topics show how to use custom controls to obtain extended functionality:

How to: Create a Custom Double-Click Event

How to: Create a Custom Image Button Control

How to: Display a Gradient Fill

How to: Create a Numeric Text Box

How to: Create an Owner-Drawn List Box

How to: Create OnEnter Functionality

How to: Scroll Form Contents

How to: Sort ListView Items

How to: Subclass a TreeView by Using Native Callbacks

How to: Subclass a Button by Using Native Callbacks

See Also

Concepts

Subclassing Controls with a Managed Window Procedure

Other Resources

Windows Forms Controls in the .NET Compact Framework