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

Posted by user1824343 on Stack Overflow See other posts from Stack Overflow or by user1824343
Published on 2012-11-14T16:53:50Z Indexed on 2012/11/14 17:00 UTC
Read the original article Hit count: 163

Filed under:

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 = "";
        }
    }

}
}

© Stack Overflow or respective owner

Related posts about c#