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.
Value property. 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.
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.
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;
}
}
}
Vertical, Horizontal. 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.
And SmallChange meanwhile determines how far the slider moves when the user presses up or down on the keyboard.
Next When you change Minimum and Maximum, the dimensions of the TrackBar will not change. However, the scale of the numbers and ticks will.
Also With Ticks, we can change how the TrackBar's tick marks are styled, and how many tick marks are drawn for the total TrackBar scale.
Summary. 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 24, 2024 (simplify).