Skip to content

Latest commit

 

History

History
95 lines (65 loc) · 5.53 KB

how-to-create-a-windows-forms-control-that-shows-progress.md

File metadata and controls

95 lines (65 loc) · 5.53 KB
title ms.date dev_langs helpviewer_keywords ms.assetid description
Create Control That Shows Progress
03/30/2017
csharp
vb
controls [Windows Forms], progress tracking
controls [Windows Forms], creating
progress [Windows Forms], reporting [Windows Forms]
FlashTrackBar custom control
24c5a2e3-058c-4b8d-a217-c06e6a130c2f
Learn how to create a Windows Forms control that uses a gradient to visually show the level or progress of an application.

How to: Create a Windows Forms Control That Shows Progress

The following code example shows a custom control called FlashTrackBar that can be used to show the user the level or the progress of an application. It uses a gradient to visually represent progress.

The FlashTrackBar control illustrates the following concepts:

  • Defining custom properties.

  • Defining custom events. (FlashTrackBar defines the ValueChanged event.)

  • Overriding the xref:System.Windows.Forms.Control.OnPaint%2A method to provide logic to draw the control.

  • Computing the area available for drawing the control by using its xref:System.Windows.Forms.Control.ClientRectangle%2A property. FlashTrackBar does this in its OptimizedInvalidate method.

  • Implementing serialization or persistence for a property when it is changed in the Windows Forms Designer. FlashTrackBar defines the ShouldSerializeStartColor and ShouldSerializeEndColor methods for serializing its StartColor and EndColor properties.

The following table shows the custom properties defined by FlashTrackBar.

Property Description
AllowUserEdit Indicates whether the user can change the value of the flash track bar by clicking and dragging it.
EndColor Specifies the ending color of the track bar.
DarkenBy Specifies how much to darken the background with respect to the foreground gradient.
Max Specifies the maximum value of the track bar.
Min Specifies the minimum value of the track bar.
StartColor Specifies the starting color of the gradient.
ShowPercentage Indicates whether to display a percentage over the gradient.
ShowValue Indicates whether to display the current value over the gradient.
ShowGradient Indicates whether the track bar should display a color gradient showing the current value.
- Value Specifies the current value of the track bar.

The following table shows additional members defined by FlashTrackBar: the property-changed event and the method that raises the event.

Member Description
ValueChanged The event that is raised when the Value property of the track bar changes.
OnValueChanged The method that raises the ValueChanged event.

Note

FlashTrackBar uses the xref:System.EventArgs class for event data and xref:System.EventHandler for the event delegate.

To handle the corresponding EventName events, FlashTrackBar overrides the following methods that it inherits from xref:System.Windows.Forms.Control?displayProperty=nameWithType:

  • xref:System.Windows.Forms.Control.OnPaint%2A

  • xref:System.Windows.Forms.Control.OnMouseDown%2A

  • xref:System.Windows.Forms.Control.OnMouseMove%2A

  • xref:System.Windows.Forms.Control.OnMouseUp%2A

  • xref:System.Windows.Forms.Control.OnResize%2A

To handle the corresponding property-changed events, FlashTrackBar overrides the following methods that it inherits from xref:System.Windows.Forms.Control?displayProperty=nameWithType:

  • xref:System.Windows.Forms.Control.OnBackColorChanged%2A

  • xref:System.Windows.Forms.Control.OnBackgroundImageChanged%2A

  • xref:System.Windows.Forms.Control.OnTextChanged%2A

Example

The FlashTrackBar control defines two UI type editors, FlashTrackBarValueEditor and FlashTrackBarDarkenByEditor, which are shown in the following code listings. The HostApp class uses the FlashTrackBar control on a Windows Form.

[!code-csharpSystem.Windows.Forms.FlashTrackBar#1] [!code-vbSystem.Windows.Forms.FlashTrackBar#1]

[!code-csharpSystem.Windows.Forms.FlashTrackBar#10] [!code-vbSystem.Windows.Forms.FlashTrackBar#10]

[!code-csharpSystem.Windows.Forms.FlashTrackBar#20] [!code-vbSystem.Windows.Forms.FlashTrackBar#20]

[!code-csharpSystem.Windows.Forms.FlashTrackBar#30] [!code-vbSystem.Windows.Forms.FlashTrackBar#30]

See also