articles

Home / DeveloperSection / Articles / How to Use Track Bar Control in C#

How to Use Track Bar Control in C#

How to Use Track Bar Control in C#

Anonymous User 31378 21-Jan-2011

A TrackBar control provides a simple interface that allows the user to set a value from a fixed range of values by graphically manipulating a slider with the mouse or keyboard.

TrackBar control has two parts. Slider and, and the tick marks. The slider is the part that can be adjusted with its position corresponding to the Value property. The tick marks are visual indicators that are spaced at regular intervals.

This example demonstrates the use of the track bar. In this example, I had taken two labels which shows message such as values in centigrade and values in farehenhite. I had taken two textbox’s also. In the first text box I enter the value in centigrade and in the second text box we get output in farehenhite. A TrackBar control shows the value in centigrade. If the value in textbox1 is entered 29 then slider moves on 29th division and appropriate farehenhite a value is shown in textbox. As well as we move the slider on TrackBar then it also changes the value in centigrade as well as farehenhite.

Useful properties of TrackBar Control

  •  Maximum The maximum value for the position of the slider on the TrackBar control. This property takes an integer types value.
  •  Minimum The minimum value for the position of the slider on the TrackBar control. This property takes an integer types value.
  •  Orientation The orientation of TrackBar control. That should be either horizontal or vertical.
  •  Large Change The number of positions the slider moves in the response of mouse click page up and page down button.
  •  Small Change The number of positions the slider moves in the response to keyboard value.
  •  Value The position of the slider on TrackBar control. 
private void txtCentiGrade_TextChanged(object sender, EventArgs e)//Text change event of the textbox control
            //as well as we made any changes in textbox control then text change event is called
        {
            if (txtCentiGrade.Text.Trim().Length > 0)//count the length of the textbox control.If it is greater than
                //0 then value of textbox control is converted into farhenhite.
            {
                try
                {
                    int centigrade = Convert.ToInt32(txtCentiGrade.Text);//string values is converted in integer format
                    if (centigrade > 300)//maximum value in centigrade that track bar control can show.
                    {
                        MessageBox.Show("The value in a centigrade should be less than 300");
                        return;
                    }
                    float forehenhite = changeForehenHite(centigrade);//call changeForhenhite(int centigrade) methods
                    //which convert centigrade values in farhenhite values.
                    slider1.Value = centigrade;//moves slider on appropriate position on trackbar control.
                    txtForehenHite.Text = forehenhite.ToString();//display values in second textbox control.
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }
            }
            else
            {
                slider1.Value = 0;//if length of the textbox1 is zero then it store the position of the slider on 0
                txtForehenHite.Text = "";
            }
        }
        publicfloat changeForehenHite(int centigrade)//A method which convert centigrade values in farhenhite values.
        {
            float forehenhite = 0;
            forehenhite = ((centigrade * 9) / 5) + 32;
            return forehenhite;           
        }
        privatevoid slider1_Scroll(object sender, EventArgs e)//When we moves slider on trackbar control then
            //slider1_Scroll() event is called.
        {
            float num = changeForehenHite(slider1.Value);
            txtForehenHite.Text = num.ToString();
            txtCentiGrade.Text = slider1.Value.ToString();
        }
    }




Updated 04-Sep-2020
I am a content writter !

Leave Comment

Comments

Liked By