Introduction
Trackbar allows users to provide input by dragging a slider on it.
Here are some important properties that are useful while working with trackbar controls:
Property | Description |
Maximum | Maximum value of the position for the trackbar slider |
Minimum | Minimum value of the position for the trackbar slider |
Orientation | Can set the trackbar orientation to Horizontal or Vertical. The default orientation is Horizontal. |
TickFrequency | No. of positions between the tick marks of the slider |
Value | Position of the slider |
Let's consider one simple example:
Drag and drop 3 trackbar controls from the toolbox to form control.
Set the Maximum property of each trackbar control to 255 and the Minimum property to 1.
Now, drag a panel control to the form control
Double click on the first trackbar. It will generate a “Scroll” event. The Scroll event executes whenever the user scrolls the slider of the trackbar.
Now, add the following code to generate a “Scroll” event of the trackbar
- panel1.BackColor = Color.FromArgb(trackBar1.Value, trackBar2.Value, trackBar3.Value);
Some important points:
- FFrom.Argb(int,int,int) Method creates a colour structure from specified 8-bit colour values (Red, Green, Blue).
- BBackColor is a property of trackbar. It is used to set the background colour for the trackbar. Almost every control has this property.
Do this for the remaining two trackbar controls. Your code will look this:
- private void trackBar1_Scroll(object sender, EventArgs e) {
- panel1.BackColor = Color.FromArgb(trackBar1.Value, trackBar2.Value, trackBar3.Value);
- }
- private void trackBar2_Scroll(object sender, EventArgs e) {
- panel1.BackColor = Color.FromArgb(trackBar1.Value, trackBar2.Value, trackBar3.Value);
- }
- private void trackBar3_Scroll(object sender, EventArgs e) {
- panel1.BackColor = Color.FromArgb(trackBar1.Value, trackBar2.Value, trackBar3.Value);
- }
Now, run the program & move all three sliders, as the sliders move, they will change the background colour of the panel.