Area Converter in Windows Phone

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

Unit Converter Part - III (Area Converter)

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

Area-Converter1.jpg

Step 1

First we create buttons to enter the values, a TextBox to show the result and a ListBox to show the sub units of the Speed Unit like this:

Area-Converter2.jpg

Step 2

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

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

public class AreaUnit

{
    public string AreaUnits { get; set;}

} 

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

public class AreaList: List<AreaUnit>

{
      public AreaList()

      {

            Add(new AreaUnit { AreaUnits = "Millimeter square" });

            Add(new AreaUnit { AreaUnits = "Centimeter square" });

            Add(new AreaUnit { AreaUnits = "Meter square " });

            Add(new AreaUnit { AreaUnits = "Inch square" });

            Add(new AreaUnit { AreaUnits = "Foot square" });

            Add(new AreaUnit { AreaUnits = "Yard square" });           

      }

 }

Step 3

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

public Area()

{
      InitializeComponent();

      FillAreaBox();

}

private
void FillAreaBox()

{

     lstFrom.ItemsSource = new AreaList();

     lstTo.ItemsSource = new AreaList();

}

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

                if (lstFrom.SelectedIndex.Equals(1))

                {

                    btnLstFrom.Content = "Centimeter square";

                }

                if (lstFrom.SelectedIndex.Equals(2))

                {

                    btnLstFrom.Content = "Meter square";

                }
 

                if (lstFrom.SelectedIndex.Equals(3))

               {
                    btnLstFrom.Content = "Inch square";

                }

                if (lstFrom.SelectedIndex.Equals(4))

                {

                    btnLstFrom.Content = "Foot square";

                }

                if (lstFrom.SelectedIndex.Equals(5))

                {

                    btnLstFrom.Content = "Yard square";

                }
 

                if (txtTo.Text == "")

                {
                    txtTo.Text = "1";

                }
 

                // Millimeter square // 

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Millimeter square"))))

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

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                   
else

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Centimeter square"))))

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

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Meter square"))))
                {
                  

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

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Inch square"))))

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

                 }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Foot square"))))

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

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Yard square"))))

                {

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

                }

Here we use only one example (Square Millimeters). (The complete code is available in the project). So when we enter a value into the TextBox and click on the To ListBox and select some Unit from there.

The Output Will Be:

Area-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 = "Millimeter square";

                   
//txtLstTo.Text = "Millimeters";

                    if (btnLstFrom.Content.Equals("Millimeter square"))

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                    if (btnLstFrom.Content.Equals("Centimeter square"))
                    {                      
                            txtTo.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 100);
                       

                    }

                    if (btnLstFrom.Content.Equals("Meter square"))

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

                    }

                    if (btnLstFrom.Content.Equals("Inch square"))

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

                    }

                    if (btnLstFrom.Content.Equals("Foot square"))

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

                    }

                    if (btnLstFrom.Content.Equals("Yard square"))

                    {

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


            }

     }     

     

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:

Area-Converter4.jpg

Now we enter some value in the empty TextBox :

Area-Converter5.jpg

Now we enter some value in the empty TextBox :

Area-Converter6.jpg

private void txtFrom_TextChanged(object sender, TextChangedEventArgs e)

{    

       txtError.Visibility = Visibility.Collapsed;

            if (txtFrom.Text == "")
            {

                txtFrom.Text = "";

                txtTo.Text = "";

            }

           
else

            {

               
// Millimeter square //
 
                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Millimeter square"))))

                {

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

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                   
else

                    {

                        txtTo.Text = txtFrom.Text;

                    }

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Centimeter square"))))

                {                   

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

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Meter square"))))

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

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Inch square"))))

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

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Foot square"))))

                {                  

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

                }

                if ((btnLstFrom.Content.Equals("Millimeter square") && (btnLstTo.Content.Equals("Yard square"))))

                {                  

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

               }              
   
}