Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I'm trying to write code for a program(windows phone 7) that will convert miles, meters, kilometers.
Everytime I try to run it it crashes. I'm new at this.

Here is my code.... I have 3 text boxes and what I'm trying to do is if I put a number in any one box that the other two boxes will so the conversion.
C#
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            decimal kilotoal;
            decimal milettl;
            decimal metttl;

          kilotoal = Convert.ToDecimal(KilometersBlock.Text);
          milettl = Convert.ToDecimal(txtmile.Text);
          metttl = Convert.ToDecimal(MetreBlock.Text);

               kilotoal = milettl*1.609m;
               metttl = milettl * 1609.3m;
               kilotoal = metttl / 1000m;
               milettl = metttl * 0.00062137119m;
             milettl = kilotoal * 0.6214m;
             metttl = kilotoal * 1000m;

            answer.Text= milettl.ToString();
            answer.Text = metttl.ToString();
            answer.Text = kilotoal.ToString();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            txtmile.Text = "";
            KilometersBlock.Text = "";
            MetreBlock.Text = "";

        }
    }
}
Posted
Updated 8-May-11 19:42pm
v3

1 solution

C#
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void txtMeter_TextChanged(object sender, EventArgs e)
        {
            if (flag)
            {
                try
                {
                    txtKiloMeter.Text = String.Format("{0:###0.000}", (double.Parse(txtMeter.Text) / 1000));
                }
                catch (Exception)
                {
                    txtKiloMeter.Text = "";
                }
            }
        }

        private void txtKiloMeter_TextChanged(object sender, EventArgs e)
        {
            if (!flag)
            {
                try
                {
                    txtMeter.Text = String.Format("{0:###0.000}", (double.Parse(txtKiloMeter.Text) * 1000));
                }
                catch (Exception)
                {
                    txtMeter.Text = "";
                }
            }
        }
        bool flag = false;
        private void txtMeter_Enter(object sender, EventArgs e)
        {
            flag = true;
        }

        private void txtMeter_Leave(object sender, EventArgs e)
        {
            flag = false;
        }


    }
}

Sumant Kumar (Software Engineer)
 
Share this answer
 
v2
Comments
Pete O'Hanlon 8-May-11 15:32pm    
Leaving your email address in an open indexed forum is a really great way to get spammed.
Member 7905463 8-May-11 16:48pm    
I forgot to add that this is for a Window Phone 7 application...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900