ProgressBarRenderer Class

Note: This class is new in the .NET Framework version 2.0.

Provides methods used to render a progress bar control with visual styles. This class cannot be inherited.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in

Public NotInheritable Class ProgressBarRenderer
Dim instance As ProgressBarRenderer

public final class ProgressBarRenderer
public final class ProgressBarRenderer

The ProgressBarRenderer class provides a set of static methods that can be used to render a progress bar control with the current visual style of the operating system. Rendering a control refers to drawing the user interface of a control. This is useful if you are drawing a custom control that should have the appearance of the current visual style. To draw a progress bar, use the DrawHorizontalBar or DrawVerticalBar methods to draw the empty bar, and then use the DrawHorizontalChunks or DrawVerticalChunks methods to draw the elements that fill in the bar.

If visual styles are enabled in the operating system and visual styles are applied to the client area of application windows, the methods of this class will draw the progress bar with the current visual style. Otherwise, the methods and properties of this class will throw an InvalidOperationException. To determine whether the members of this class can be used, you can check the value of the IsSupported property.

This class wraps the functionality of a System.Windows.Forms.VisualStyles.VisualStyleRenderer that is set to one of the elements exposed by the System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.Bar, System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.BarVertical, System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.Chunk, and System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar.ChunkVertical classes. For more information, see Rendering Controls with Visual Styles.

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.

The following code example demonstrates how to create a custom control that uses the DrawVerticalBar and DrawVerticalChunks methods to draw a vertical progress bar. The control uses a Timer to redraw the progress bar with an added piece each second. The SetupProgressBar method uses the ChunkThickness and ChunkSpaceThickness properties to calculate the height of each progressively larger rectangle that is drawn.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Public Class Form1
    Inherits Form
    Private bar1 As New VerticalProgressBar()
    Private button1 As New Button()
    Public Sub New() 
        Me.Size = New Size(500, 500)
        bar1.NumberChunks = 30
        button1.Location = New Point(150, 10)
        button1.Size = New Size(150, 30)
        button1.Text = "Start VerticalProgressBar"
        AddHandler button1.Click, AddressOf button1_Click
        Controls.AddRange(New Control() {button1, bar1})
    End Sub 'New
    <STAThread()>  _
    Public Shared Sub Main() 
        ' The call to EnableVisualStyles below does not affect
        ' whether ProgressBarRenderer.IsSupported is true; as 
        ' long as visual styles are enabled by the operating system, 
        ' IsSupported is true.
        Application.Run(New Form1())
    End Sub 'Main
    ' Start the VerticalProgressBar.
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) 
    End Sub 'button1_Click
End Class 'Form1

Public Class VerticalProgressBar
    Inherits Control
    Private numberChunksValue As Integer
    Private ticks As Integer
    Private progressTimer As New Timer()
    Private progressBarRectangles() As Rectangle
    Public Sub New() 
        Me.Location = New Point(10, 10)
        Me.Width = 50
        ' The progress bar will update every second.
        progressTimer.Interval = 1000
        AddHandler progressTimer.Tick, AddressOf progressTimer_Tick
        ' This property also calls SetupProgressBar to initialize 
        ' the progress bar rectangles if styles are enabled.
        NumberChunks = 20
        ' Set the default height if visual styles are not enabled.
        If Not ProgressBarRenderer.IsSupported Then
            Me.Height = 100
        End If
    End Sub 'New
    ' Specify the number of progress bar chunks to base the height on.
    Public Property NumberChunks() As Integer 
            Return numberChunksValue
        End Get
            If value <= 50 AndAlso value > 0 Then
                numberChunksValue = value
                MessageBox.Show("Number of chunks must be between " + "0 and 50; defaulting to 10")
                numberChunksValue = 10
            End If
            ' Recalculate the progress bar size, if visual styles 
            ' are active.
            If ProgressBarRenderer.IsSupported Then
            End If
        End Set
    End Property
    ' Draw the progress bar in its normal state.
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
        If ProgressBarRenderer.IsSupported Then
            ProgressBarRenderer.DrawVerticalBar(e.Graphics, ClientRectangle)
            Me.Parent.Text = "VerticalProgressBar Enabled"
            Me.Parent.Text = "VerticalProgressBar Disabled"
        End If
    End Sub 'OnPaint
    ' Initialize the rectangles used to paint the states of the 
    ' progress bar.
    Private Sub SetupProgressBar() 
        If Not ProgressBarRenderer.IsSupported Then
        End If
        ' Determine the size of the progress bar frame.
        Me.Size = New Size(ClientRectangle.Width, NumberChunks *(ProgressBarRenderer.ChunkThickness + 2 * ProgressBarRenderer.ChunkSpaceThickness) + 6)
        ' Initialize the rectangles to draw each step of the 
        ' progress bar.
        progressBarRectangles = New Rectangle(NumberChunks) {}
        Dim i As Integer
        For i = 0 To NumberChunks
            ' Use the thickness defined by the current visual style 
            ' to calculate the height of each rectangle. The size 
            ' adjustments ensure that the chunks do not paint over 
            ' the frame.
            Dim filledRectangleHeight As Integer = (i + 1)  _
		*(ProgressBarRenderer.ChunkThickness + 2 * ProgressBarRenderer.ChunkSpaceThickness)
            progressBarRectangles(i) = New Rectangle(ClientRectangle.X + 3, _
                ClientRectangle.Y + ClientRectangle.Height - 3 - filledRectangleHeight, _
                ClientRectangle.Width - 6, filledRectangleHeight)
        Next i
    End Sub 'SetupProgressBar
    ' Handle the timer tick; draw each progressively larger rectangle.
    Private Sub progressTimer_Tick(ByVal myObject As [Object], ByVal e As EventArgs) 
        If ticks < NumberChunks Then
            Dim g As Graphics = Me.CreateGraphics()
                ProgressBarRenderer.DrawVerticalChunks(g, progressBarRectangles(ticks))
                ticks += 1
            End Try
            progressTimer.Enabled = False
        End If
    End Sub 'progressTimer_Tick
    ' Start the progress bar.
    Public Sub Start() 
        If ProgressBarRenderer.IsSupported Then
            MessageBox.Show("VerticalScrollBar requires visual styles")
        End If
    End Sub 'Start
End Class 'VerticalProgressBar


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions