Custom Drawing User Control Sample

To get samples and instructions for installing them, see the following:

  • Click Samples on the Visual Studio Help menu.

    For more information, see Locating Sample Files.

  • The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.

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.