How to use Track Bar Control in C#

Posted by  Awadhendra Tiwari
on    January-21-2011 15:31 PM

C# C# 
Ratings:
0 Comment(s)
 13763  View(s)
Rate this:

How to use TrackBar Control in C#

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.

How to use Track Bar Control in C#

The 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 which are spaced at regular intervals.

This example demonstrates the use of 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 first text box I enter value in centigrade and in 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 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 position the slider moves in response of mouse click page up and page down button.

·         Small Change The number of positions the slider moves in response of 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 = "";

            }

        }

        public float changeForehenHite(int centigrade)//A method which convert centigrade values in farhenhite values.

        {

            float forehenhite = 0;

            forehenhite = ((centigrade * 9) / 5) + 32;

            return forehenhite;           

        }

        private void 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();

        }

    }
















Recent Activities

jayprakash sharma Created New Discussion Extracting texts from html tags   22 hours ago

Anchal Kesharwani added new Blog How to Use JSON   23 hours ago

Sumit Kesarwani added new Article Custom Error Page in Asp.Net Mvc 4   yesterday


Top Contributors

Don't want to miss updates? Please click the below button!