C# – How to use TrackBar control?

TrackBar control is one of the useful user interface control, commonly used to select a value from the given range, by using it’s slider (or thumb) or by using keyboard keys. For example, if you want to set the brightness of the screen, using your program, you can use this control as an interface element and attach to the background logic to set the brightness of the screen whenever the control’s value changed.

Properties

By using TrackBar control’s Minimum & Maximum properties, we can set the boundaries of the values (range) the user can select using this control. The values are integer values. Lower limit of the range, set through Minimum property & upper limit of the range, set through it’s Maximum property.

TrackBar control provides a slider to allow the user to select the value from the set range, of the control. When the user moves the slider, control’s Value property will be changed to keep the current value of the control. User can move the slider using the Mouse or Keyboard keys.

Note that, the value set, must be with in the set range; otherwise, the control will throw the Error; “System.ArgumentOutOfRangeException”. ‘Value’ should be between ‘Minimum’ and ‘Maximum’.

Another useful, SmallChange & LargeChange properties to control the number of positions the slider moves when the user press the keys on the keyboard. SmallChange property controls the number of positions of the slider, when the user press the arrow keys on the keyboard; whereas LargeChange property controls when the user clicks the mouse or press the Page Up / Page Down keys from the keyboard.

Tick marks also will be displayed on the control to indicate the position of the slider. TickStyle property is used to control these tick marks. It controls where to display the tick marks on the control; whether BottomRight or TopLeft or Both sides of the control. By setting, None value to this property, we can hide the tick marks on the control.

When tick marks are visible by setting TickStyle property, another property we can use is TickFrequency to set the number of positions between the tick marks.

By using Orientation property, we can display the control Horizontal or in Vertical direction.

Methods

TrackBar control’s SetRange method is useful to set the Minimum & Maximum values for the control. We can set the range of values for the control, by using above mentioned Minimum & Maximum properties or by calling this SetRange method.

Events

When the TrackBar control’s value is changed, it triggers ValueChanged event.

Complete working code

Now this is the time to put all the things we discussed so far, together in one place and finally here is the working example;

Designer C# file

C# code file

Upon successful execution of the code, you will see the Application window, like below;

TrackBar control - demo
TrackBar control – demo

(Raju)

C# – How to use TrackBar control?

Leave a Reply

Your email address will not be published.

Scroll to top