TrackBar
This provides a slider control. It allows the user to select a value from the slider by dragging on it. We change many aspects of the TrackBar
, including its orientation and appearance.
With the trackbar, we can access the Value property to get a result from the user interaction. We can then use this value elsewhere in the program.
To begin, let's add a TrackBar
control. 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
.
trackBar1_Scroll
, you can mutate other properties in your program based on the Value property of the TrackBar
.TrackBar
is scrolled.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; } } }
You may want to use a vertical TrackBar
instead of a horizontal TrackBar
. You can change the Orientation property to Vertical instead of the default, which is Horizontal.
LargeChange
, SmallChange
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
meanwhile determines how far the slider moves when the user presses up or down on the keyboard.TrackBar
will not change. However, the scale of the numbers and ticks will.TrackBar
's tick marks are styled, and how many tick marks are drawn for the total TrackBar
scale.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.