Custom Drawing User Control Sample

This sample demonstrates how to create and use custom-drawn user controls.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To run this sample

  • Press F5.

Demonstrates

This sample demonstrates how to create custom-drawn user controls, use them on a form, have the control respond to events, and define properties for use in the designer. The controls that are created are score-keeping controls. One is a digital scoreboard which displays a number in a digital format. The other is a beaded scoreboard which slides beads back and forth to track a score. Both respond to various events and have numerous properties available at design time.

  • Custom Drawing   Both custom controls in this sample demonstrate how to inherit from the UserControl class and override OnPaint. A custom-drawn user control inherits from UserControl. The custom drawing occurs by overriding the OnPaint method of the control. The OnPaint method provides the PaintEventArgs argument from which the clip rectangle (Rectangle) and the graphics object (Graphics) are obtained.

  • Custom Control Events   Custom controls can respond to the events that a normal control would respond to. This is done by overriding the event handler method in the UserControl class. Both custom controls in this sample demonstrate how to respond to various events such as MouseUp and Click.

  • **Properties   **It is often desirable to create properties for a control that can be edited at design time using Visual Studio. Both custom controls in this sample show how to create Public properties that will appear in the Properties window in Visual Studio.

See Also

Tasks

Drawing an Image on a Form

Making Graphics Appear

Other Resources

Custom Control Painting and Rendering