TrackBar

TrackBar provides a slider control. It allows the user to select a value from the slider by dragging on it. In Visual Studio we change many aspects of the TrackBar, including its orientation and appearance. We add an event handler to execute code whenever the TrackBar is used.

This C# tutorial describes the TrackBar control in Windows Forms.

TrackBar control screenshot

Get started

Steps

To begin, let's add a TrackBar control to your Windows Forms program in the Visual Studio designer. Make sure the Toolbox is open and double-click on the TrackBar icon. Next, you can adjust properties on the TrackBar by right-clicking it and selecting Properties. Further, you can add event handlers by clicking on the lightning bolt.

Vertical and horizontal

In some program contexts, you may want to use a vertical TrackBar instead of a horizontal TrackBar. Fortunately, the Windows Forms developers thought of this and you can change the Orientation property to Vertical instead of the default, which is Horizontal. Please notice the layout of the TrackBar in the following screenshot: it is laid out vertically.

TrackBar orientation

LargeChange and SmallChange

There are two properties on the TrackBar that indicate what intervals the slider changes when the user clicks on it or presses a key. The LargeChange integer indicates the number of steps that are taken when the page up or page down keys are pressed, and also when the user clicks on a part of the TrackBar where the slider is not located. The SmallChange integer, on the other hand, simply determines how far the slider moves when the user presses up or down on the keyboard.

(SmallChange does not have to be smaller than LargeChange.)

Minimum and Maximum

You can also set the Minimum and Maximum properties on the TrackBar in the Properties window. When you change Minimum and Maximum, the dimensions of the TrackBar will not change. However, the scale of the numbers and ticks will. The TrackBar will simply resize its scale to suit the Minimum and Maximum you choose.

TickStyle and TickFrequency

The TrackBar also allows you to change how its tick marks are styled, and how many tick marks are drawn for the total TrackBar scale. If you change TickStyle to Both, you can have ticks on both the top and bottom of the TrackBar. If you increase the TickFrequency past the value of one, you can reduce the number of tick lines that are drawn; this can create a more appealing visual effect.

TrackBar: TickFrequency and TickStyle

Scroll event handler

One of the most important event handlers that is available on the TrackBar is the Scroll event handler. To create the Scroll event handler, you can double-click on the TrackBar when you see it in Windows Forms. In the body of the method trackBar1_Scroll, you can mutate other properties in your program based on the Value property of the TrackBar. In this program, we change the window title bar whenever the TrackBar is scrolled.

Program that uses Scroll event handler [C#]

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void trackBar1_Scroll(object sender, EventArgs e)
	{
	    // Set the window title text each time a scroll occurs.
	    this.Text = "Value: " + trackBar1.Value;
	}
    }
}
TrackBar event handler

Summary

It is perhaps one the less-used Windows Forms controls. But the TrackBar can be valuable in some programs. It is a versatile and configurable control and fits well with the concept of Windows Forms as a framework. The TrackBar can be adjusted through its various properties, and you can mutate other parts of your program through the event handler system.

Windows Forms
.NET