locked
Slider double value RRS feed

  • Question

  • hey guys,

    I have a slider and a textbox,  binded the value to a textbox as the slider slides

    the problem is that when I slide I get a double values which is shown in the textbox . I want an integer values to be shown

    the min value of my slider is from 0 to 120.

    2nd Problem.

    suppose I get a value of 20(integer) on the textbox , now I want this integer value .can u tell me a method name for the textbox  which gets me the shown integer values displayed so that i can further put the  value in a variable and finally put that in the formula . I want the code in C#

    Thanks in advance  

    Monday, March 24, 2014 6:37 PM

Answers

  • Hello,

    When the slider value changes you get a "Value" that is of type double. To convert the double to an integer you can use the Math.Round(...) function to round the double to the nearest integer.

    For the second question, doesn't the text box' .Text property get you the value inside the textbox? If not please clarify your second question.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by Naman Joshi Tuesday, March 25, 2014 4:27 PM
    Tuesday, March 25, 2014 12:36 AM
  • You can round the slider value and update the slider with the rounded value:

      <Slider x:Name="slider" Minimum="1" Maximum="10" ValueChanged="slider_ValueChanged" />


            void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
                if (slider != null)
                {
                    slider.Value = Math.Round(slider.Value);
                }
            }

    To convert the TextBox value to an int, call int.TryParse().

                int parsedInt;
                bool succeed = int.TryParse(textbox.Text, out parsedInt);

    If the text represents a valid int value, succeed will be set to true and parsedInt will receive the int value. Otherwise succeed will be set to false.


    • Edited by mSpot Inc Tuesday, March 25, 2014 4:42 AM
    • Proposed as answer by Kulasangar Tuesday, March 25, 2014 6:34 AM
    • Marked as answer by Naman Joshi Tuesday, March 25, 2014 4:27 PM
    Tuesday, March 25, 2014 4:40 AM
  • i have used:

    double a=ConvertToInt32(txtbox.Text);

    is this  the correct logical way . ?
    It will work. The framework has several different conversion API and any of them can do the job. However, you must validate the input or handle exceptions if the input string doesn't represent an integer. Using TryParse, you can blindly send in the text and simply check the return code.
    • Marked as answer by Naman Joshi Tuesday, March 25, 2014 5:32 PM
    Tuesday, March 25, 2014 5:02 PM

All replies

  • Hello,

    When the slider value changes you get a "Value" that is of type double. To convert the double to an integer you can use the Math.Round(...) function to round the double to the nearest integer.

    For the second question, doesn't the text box' .Text property get you the value inside the textbox? If not please clarify your second question.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by Naman Joshi Tuesday, March 25, 2014 4:27 PM
    Tuesday, March 25, 2014 12:36 AM
  • You can round the slider value and update the slider with the rounded value:

      <Slider x:Name="slider" Minimum="1" Maximum="10" ValueChanged="slider_ValueChanged" />


            void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
                if (slider != null)
                {
                    slider.Value = Math.Round(slider.Value);
                }
            }

    To convert the TextBox value to an int, call int.TryParse().

                int parsedInt;
                bool succeed = int.TryParse(textbox.Text, out parsedInt);

    If the text represents a valid int value, succeed will be set to true and parsedInt will receive the int value. Otherwise succeed will be set to false.


    • Edited by mSpot Inc Tuesday, March 25, 2014 4:42 AM
    • Proposed as answer by Kulasangar Tuesday, March 25, 2014 6:34 AM
    • Marked as answer by Naman Joshi Tuesday, March 25, 2014 4:27 PM
    Tuesday, March 25, 2014 4:40 AM
  • Thanks mate .it worked nicely :)
    Tuesday, March 25, 2014 4:27 PM
  • i have used:

    double a=ConvertToInt32(txtbox.Text);

    is this  the correct logical way . ?
    Tuesday, March 25, 2014 4:29 PM
  • i have used:

    double a=ConvertToInt32(txtbox.Text);

    is this  the correct logical way . ?
    It will work. The framework has several different conversion API and any of them can do the job. However, you must validate the input or handle exceptions if the input string doesn't represent an integer. Using TryParse, you can blindly send in the text and simply check the return code.
    • Marked as answer by Naman Joshi Tuesday, March 25, 2014 5:32 PM
    Tuesday, March 25, 2014 5:02 PM
  • Thanks alot for the explanation

    i will implement it

    peace and grace

    Tuesday, March 25, 2014 5:32 PM