TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

TrackBar: Windows Forms

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

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.

And: We add an event handler to execute code (written in C#) whenever the TrackBar is used.

Start. 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.

Vertical, Horizontal. 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.

LargeChange, SmallChange. 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.

Minimum, 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.

Note: The TrackBar will simply resize its scale to suit the Minimum and Maximum you choose.

Ticks. 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.

Scroll. 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.

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.

C# program that uses Scroll event handler

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;
	}
    }
}

 

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.

Review: The TrackBar can be adjusted through its properties. You can mutate other parts of your program through the event handler system.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf