Unit Converter Part - IV (Length Converter) in Window Phone7

This is a very basic engineering length convertor. The following units can be converted with the use of this example: Millimeters, Centimeters, Meters, Kilometers, Inches, Feet, Yards and Miles.

Unit-Converter1.jpg

Step 1

 First we create buttons to enter the numbers, a TextBox to show the result and a ListBox to show the sub units of the unit of length like this:

Unit-Converter2.jpg

Step 2 

After that we will use two classes, one for the Length List and the other one is LengthUnit. These files are basically used to fill the ListBox. First we will write the code for the Length Unit.

Here we include a .cs file (Length.cs):

public class Length

{
      public string LengthUnits { get; set; }
}

After that we will write the code for LengthList.cs:

public class LengthList:List<Length>

 {
        public LengthList()

        {

            Add(new Length { LengthUnits = "Millimeters" });

            Add(new Length { LengthUnits = "Centimeters" });

            Add(new Length { LengthUnits = "Meters" });

            Add(new Length { LengthUnits = "Kilometers" });

            Add(new Length { LengthUnits = "Inches" });

            Add(new Length { LengthUnits = "Feet" });

            Add(new Length { LengthUnits = "Yards" });

            Add(new Length { LengthUnits = "Miles" });
 

        }

    }

Step 3

After that we will write the following code to fill the ListBox in the LengthConverter.xaml.cs page like this:

public LengthConvertor()

{
          InitializeComponent();
          FillLengthBox();
  }

        private void FillLengthBox()

        {
            lstFrom.ItemsSource = new LengthList();

            lstTo.ItemsSource = new LengthList();

        }

Step 4

Now we will write the code for our From ListBox:

private void lstFrom_SelectionChanged(object sender, SelectionChangedEventArgs e)

{
            MainGrid.Visibility = Visibility.Visible;

            FromGrid.Visibility = Visibility.Collapsed;

            if (txtFrom.Text == "")

            {

                txtError.Visibility = Visibility.Visible;
\          
           
else

            {

               if (lstFrom.SelectedIndex.Equals(0))
               {

                    btnLstFrom.Content = "Millimeters";
 

                }

                if (lstFrom.SelectedIndex.Equals(1))

                {
                    btnLstFrom.Content = "Centimeters";

                }

                if (lstFrom.SelectedIndex.Equals(2))

                {

                    btnLstFrom.Content = "Meters";

                }

                if (lstFrom.SelectedIndex.Equals(3))

                {
                    btnLstFrom.Content = "Kilometers";

                }

                if (lstFrom.SelectedIndex.Equals(4))


                {

                    btnLstFrom.Content = "Inches";

                }

                if (lstFrom.SelectedIndex.Equals(5))

                {

                    btnLstFrom.Content = "Feet";

                }

                if (lstFrom.SelectedIndex.Equals(6))

                {

                    btnLstFrom.Content = "Yards";

                }

                if (lstFrom.SelectedIndex.Equals(7))

                {

                    btnLstFrom.Content = "Miles";

                }

                if (txtTo.Text == "")

                {

                    txtTo.Text = "1";

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Millimeters"))))

                {

                    if (txtFrom.Text.Contains('.'))

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                   
else

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Centimeters"))))

                {
                  
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.1);
                   
                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Meters"))))

                {
                  
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.001);
                  

                } 

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Kilometers"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.000001);
                   

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Inches"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.03937);

                 }

                 if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Feet"))))
                 {
                 

                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.003281);             

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Yards"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.001094);

                 }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Miles"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 6.21e-07);

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Centimeters"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.1);
                   

                }
  }

The Output Will Be:

Unit-Converter3.jpg

Step 5

Now we will write the code for the To ListBox:

private void lstTo_SelectionChanged(object sender, SelectionChangedEventArgs e)

{
            MainGrid.Visibility = Visibility.Visible;

            ToGrid.Visibility = Visibility.Collapsed;

            if (txtFrom.Text == "")

            {
 
                 txtError.Visibility = Visibility.Visible;

            }

           
else

            {

                txtError.Visibility = Visibility.Collapsed;

                if (lstTo.SelectedIndex.Equals(0))

                {

                    btnLstTo.Content = "Millimeters";

                   
//txtLstTo.Text = "Millimeters";

                    if (btnLstFrom.Content.Equals("Millimeters"))

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                    if (btnLstFrom.Content.Equals("Centimeters"))

                    {

                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 10);

                    }

                    if (btnLstFrom.Content.Equals("Meters"))

                    {
                     
                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1000);
                      
                    }

                    if (btnLstFrom.Content.Equals("Kilometers"))

                    {

                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1000000);
                      
                    }

                    if (btnLstFrom.Content.Equals("Inches"))

                    {
                      
                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 25.4);
                      
                    }

                    if (btnLstFrom.Content.Equals("Feet"))

                    {

                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 304.8);
                      
                    }

                    if (btnLstFrom.Content.Equals("Yards"))

                    {

                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 914.4);
                      

                    }

                    if (btnLstFrom.Content.Equals("Miles"))

                    {
                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1609344);                      

                    }

                }
      }

Step 6

Now we write the code for the TextBox (txtFrom) in which we enter the value. So when we change the value of the TextBox the result will be changed according to our From and To values like this:

private void txtFrom_TextChanged(object sender, TextChangedEventArgs e)

 {
            txtError.Visibility = Visibility.Collapsed;

            if (txtFrom.Text == "")

            {

                txtFrom.Text = "";

                txtTo.Text = "";

            }

           
else

            {

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Millimeters"))))

                {

                    if (txtFrom.Text.Contains('.'))

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                   
else

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Centimeters"))))

                {

                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.1);
                   
                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Meters"))))

                {

                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.001);
                                           
                }
 
                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Kilometers"))))

                {

                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.000001);

                }
 
                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Inches"))))

                {

                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.03937);

                }
 

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Feet"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.003281);

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Yards"))))

                {

                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.001094);

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Miles"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 6.21e-07);

                }

                if ((btnLstFrom.Content.Equals("Millimeters") && (btnLstTo.Content.Equals("Centimeters"))))

                {
                        txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.1);

                }

   }

Unit-Converter4.jpg

Now we enter some value in the empty TextBox :

Unit-Converter6.jpg


Up Next
    Ebook Download
    View all
    Learn
    View all