C# TrackBar: Windows FormsThis C# tutorial describes the TrackBar control in Windows Forms.
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.
And: We add an event handler to execute code (written in C#) whenever the TrackBar is used.
To begin, let's add a TrackBar control to your Windows Forms program. 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. Many event handlers are available.
You may want to use a vertical TrackBar instead of a horizontal TrackBar. Fortunately, the Windows Forms developers thought of this. You can change the Orientation property to Vertical instead of the default, which is Horizontal.
Note: Check the layout of the TrackBar in the following screenshot. It is laid out vertically.
These indicate what intervals the slider changes when manipulated. LargeChange is number of steps taken when the page up or page down keys are pressed, and also when the user clicks where the slider is not located.
SmallChange: This meanwhile determines how far the slider moves when the user presses up or down on the keyboard.
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.
Note: The TrackBar will simply resize its scale to suit the Minimum and Maximum you choose.
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.
And: If you increase the TickFrequency past the value of one, you can reduce the number of tick lines that are drawn.
An important event handler on the TrackBar control 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.
Tip: In the body of the method trackBar1_Scroll, you can mutate other properties in your program based on the Value property of the TrackBar.
Here: In this program, we change the window title bar whenever the TrackBar is scrolled.
C# program that uses Scroll event handler
public partial class Form1 : Form
private void trackBar1_Scroll(object sender, EventArgs e)
// Set the window title text each time a scroll occurs.
this.Text = "Value: " + trackBar1.Value;
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.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.