user1824343
user1824343

Reputation: 19

how do i make the app take correct input..?

This is my windows app's one layout which converts Celsius to Fahrenheit. The problem is that when I try to input the temperature it shows some junk(for eg: if i enter '3' it displayin '3.0000009') and sometimes its even showing stack overflow exception. The output is also not shown properly :

cel.text is the textbox for celsius. fahre.text is the textbox for fahrenheit.

namespace PanoramaApp1
{
    public partial class FahretoCel : PhoneApplicationPage
    {
    public FahretoCel()
    {
        InitializeComponent();

    }

    private void fahre_TextChanged(object sender, TextChangedEventArgs e)
    {

        if (fahre.Text != "")
        {
            try
            {
                double F = Convert.ToDouble(fahre.Text);
                cel.Text = "" + ((5.0/9.0) * (F - 32)) ; //this is conversion expression

            }

            catch (FormatException)
            {
                fahre.Text = "";
                cel.Text = "";
            }

        }
        else
        {
            cel.Text = "";
        }
    }

    private void cel_TextChanged(object sender, TextChangedEventArgs e)
    {

        if (cel.Text != "")
        {
            try
            {
                Double c = Convert.ToDouble(cel.Text);
                fahre.Text = "" + ((c *(9.0 / 5.0 )) + 32);

            }
            catch (FormatException)
            {
                fahre.Text = "";
                cel.Text = "";
            }

        }
        else
        {
            fahre.Text = "";
        }
    }

}
}

Upvotes: 1

Views: 48

Answers (2)

What is happening, is your Text_Changed event handlers are triggering each-other, and they keep changing each-other's text.

when you convert from celsius to farenheit, it converts back, and forth indefinitly.

This explains both your stack overflow error and your input's text changing.

What I would do, Is i would perform the conversion with a button OR, you can have a boolean variable which turns on or off the other event handlers.

Imagine something like this

protected bool textChangedEnabled = true;

private void cel_TextChanged(object sender, TextChangedEventArgs e)
{
    if(textChangedEnabled)
    {
        textChangedEnabled = false;
        if (cel.Text != "")
        {
            try
            {
                Double c = Convert.ToDouble(cel.Text);
                fahre.Text = "" + ((c *(9.0 / 5.0 )) + 32);

            }
            catch (FormatException)
            {
                fahre.Text = "";
                cel.Text = "";
            }

        }
        else
        {
            fahre.Text = "";
        }
        textChangedEnabled = true;
    }
}

There's probably a more elegant and more thread-safe way to accomplish it, but that's just a simple fix.

Upvotes: 2

Adil
Adil

Reputation: 148180

You can use Math.Round to round the value to desired number of place after decimal. Rounding upto zero will remove fractional part.

Change

cel.Text = "" + ((5.0/9.0) * (F - 32)) ;

To

cel.Text = Math.Round( ((5.0/9.0) * (F - 32)), 2).ToString() ;

Upvotes: 1

Related Questions