This documentation is archived and is not being maintained.

TrackBar Class

Represents a standard Windows track bar.

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

'Declaration
<ComVisibleAttribute(True)> _
<DefaultBindingPropertyAttribute("Value")> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class TrackBar _
	Inherits Control _
	Implements ISupportInitialize
'Usage
Dim instance As TrackBar

The TrackBar is a scrollable control similar to the ScrollBar control. You can configure ranges through which the value of the Value property of a track bar scrolls by setting the Minimum property to specify the lower end of the range and the Maximum property to specify the upper end of the range.

The LargeChange property defines the increment to add or subtract from the Value property when clicks occur on either side of the scroll box. The track bar can be displayed horizontally or vertically.

You can use this control to input numeric data obtained through the Value property. You can display this numeric data in a control or use it in code.

The following code example displays a form containing a TrackBar control and a TextBox control. The example demonstrates setting the Maximum, TickFrequency, LargeChange, and SmallChange properties and handling the Scroll event. The TextBox contents are updated to the Value property value when the Scroll event occurs.

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

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents trackBar1 As System.Windows.Forms.TrackBar
    Private textBox1 As System.Windows.Forms.TextBox

    <System.STAThread()> _
    Public Shared Sub Main()
        System.Windows.Forms.Application.Run(New Form1)
    End Sub 'Main

    Public Sub New()
        Me.textBox1 = New System.Windows.Forms.TextBox
        Me.trackBar1 = New System.Windows.Forms.TrackBar

        ' TextBox for TrackBar.Value update. 
        Me.textBox1.Location = New System.Drawing.Point(240, 16)
        Me.textBox1.Size = New System.Drawing.Size(48, 20)

        ' Set up how the form should be displayed and add the controls to the form. 
        Me.ClientSize = New System.Drawing.Size(296, 62)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.textBox1, Me.trackBar1})
        Me.Text = "TrackBar Example" 

        ' Set up the TrackBar. 
        Me.trackBar1.Location = New System.Drawing.Point(8, 8)
        Me.trackBar1.Size = New System.Drawing.Size(224, 45)

        ' The Maximum property sets the value of the track bar when 
        ' the slider is all the way to the right.
        trackBar1.Maximum = 30

        ' The TickFrequency property establishes how many positions 
        ' are between each tick-mark.
        trackBar1.TickFrequency = 5

        ' The LargeChange property sets how many positions to move 
        ' if the bar is clicked on either side of the slider.
        trackBar1.LargeChange = 3

        ' The SmallChange property sets how many positions to move 
        ' if the keyboard arrows are used to move the slider.
        trackBar1.SmallChange = 2
    End Sub 'New 

    Private Sub trackBar1_Scroll(ByVal sender As Object, _
                    ByVal e As System.EventArgs) Handles trackBar1.Scroll

        ' Display the trackbar value in the text box.
        textBox1.Text = trackBar1.Value
    End Sub  

End Class 'Form1

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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Show: