Unit Converter: Part I (Mass Converter)

This is a very basic engineering Mass converter. The following units can be converted with the use of this example:

Grams, Kilograms, Metric tonnes, Short ton, Long ton, Pounds, Ounces

MassConvertor1.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 Mass Unit, like Grams, etc. like this:

MassConvertor2.jpg

The following ListBox will be shown when we click on the From and To Units:

MassConvertor3.jpg

Step 2

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

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

  1. public class MassUnit  
  2. {  
  3.     public string MassUnits { getset; }  
  4. }  
After that we will write the code for MassList.cs:
  1. public class MassList : List<MassUnit>  
  2. {  
  3.     public MassList()  
  4.     {  
  5.         Add(new MassUnit { MassUnits = "Grams" });  
  6.         Add(new MassUnit { MassUnits = "Kilograms" });  
  7.         Add(new MassUnit { MassUnits = "Metric tonnes" });  
  8.         Add(new MassUnit { MassUnits = "Short ton" });  
  9.         Add(new MassUnit { MassUnits = "Long ton" });  
  10.         Add(new MassUnit { MassUnits = "Pounds" });  
  11.         Add(new MassUnit { MassUnits = "Ounces" });  
  12.     }  
  13. }  
Step 3

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

  1. public Mass()  
  2. {  
  3.     InitializeComponent();  
  4.     FillMassBox();  
  5. }  
  6. private void FillMassBox()  
  7. {  
  8.     lstFrom.ItemsSource = new MassList();  
  9.     lstTo.ItemsSource = new MassList();  
  10. }  
Step 4

Now we will write the code for our From ListBox:

  1. private void lstFrom_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  2. {  
  3.     MainGrid.Visibility = Visibility.Visible;  
  4.     FromGrid.Visibility = Visibility.Collapsed;  
  5.     if (txtFrom.Text == "")  
  6.     {  
  7.         txtError.Visibility = Visibility.Visible;  
  8.     }  
  9.     else  
  10.     {  
  11.         if (lstFrom.SelectedIndex.Equals(0))  
  12.         {  
  13.             btnLstFrom.Content = "Grams";  
  14.         }  
  15.         if (lstFrom.SelectedIndex.Equals(1))  
  16.         {  
  17.             btnLstFrom.Content = "Kilograms";  
  18.         }  
  19.         if (lstFrom.SelectedIndex.Equals(2))  
  20.         {  
  21.             btnLstFrom.Content = "Metric tonnes";  
  22.         }  
  23.         if (lstFrom.SelectedIndex.Equals(3))  
  24.         {  
  25.             btnLstFrom.Content = "Short ton";  
  26.         }  
  27.         if (lstFrom.SelectedIndex.Equals(4))  
  28.         {  
  29.             btnLstFrom.Content = "Long ton";  
  30.         }  
  31.         if (lstFrom.SelectedIndex.Equals(5))  
  32.         {  
  33.             btnLstFrom.Content = "Pounds";  
  34.         }  
  35.         if (lstFrom.SelectedIndex.Equals(6))  
  36.         {  
  37.             btnLstFrom.Content = "Ounces";  
  38.         }  
  39.         if (txtResult.Text == "")  
  40.         {  
  41.             txtResult.Text = "1";  
  42.         }  
  43.         // Grams //  
  44.         if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Grams"))))  
  45.         {  
  46.             if (txtFrom.Text.Contains('.'))  
  47.             {  
  48.                 txtResult.Text = txtFrom.Text;  
  49.             }  
  50.             else  
  51.             {  
  52.                 txtResult.Text = txtFrom.Text;  
  53.             }  
  54.         }  
  55.         if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Kilograms"))))  
  56.         {  
  57.             txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.001);  
  58.         }  
  59.         if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Metric tonnes"))))  
  60.         {  
  61.             txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.000001);  
  62.         }  
  63.         if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Short ton"))))  
  64.         {  
  65.             txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.000001);  
  66.         }  
  67.         if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Long ton"))))  
  68.         {  
  69.             txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 9.84e-07);  
  70.         }  
  71.         if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Pounds"))))  
  72.         {  
  73.             txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.002205);  
  74.         }  
  75.         if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Ounces"))))  
  76.         {  
  77.             txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.035273);  
  78.         }  
  79.     }  
  80. }  
Here we take only one example (Gram Unit). (The complete code is available in the project.) So when we enter a value in the TextBox and click on the To ListBox and select some Unit from there,

the output will be:

MassConvertor4.jpg

MassConvertor5.jpg

Step 5

Now we will write the code for the To ListBox:

  1. private void lstTo_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  2. {  
  3.     MainGrid.Visibility = Visibility.Visible;  
  4.     ToGrid.Visibility = Visibility.Collapsed;  
  5.     if (txtFrom.Text == "")  
  6.     {  
  7.         txtError.Visibility = Visibility.Visible;  
  8.     }  
  9.     else  
  10.     {  
  11.         txtError.Visibility = Visibility.Collapsed;  
  12.         if (lstTo.SelectedIndex.Equals(0))  
  13.         {  
  14.             btnLstTo.Content = "Grams";  
  15.             //txtLstTo.Text = "Millimeters";  
  16.             if (btnLstFrom.Content.Equals("Grams"))  
  17.             {  
  18.                 txtResult.Text = txtFrom.Text;  
  19.             }  
  20.             if (btnLstFrom.Content.Equals("Kilograms"))  
  21.             {  
  22.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1000);  
  23.             }  
  24.             if (btnLstFrom.Content.Equals("Metric tonnes"))  
  25.             {  
  26.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1000000);  
  27.             }  
  28.             if (btnLstFrom.Content.Equals("Short ton"))  
  29.             {  
  30.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 907200);  
  31.             }  
  32.             if (btnLstFrom.Content.Equals("Long ton"))  
  33.             {  
  34.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1016000);  
  35.             }  
  36.             if (btnLstFrom.Content.Equals("Pounds"))  
  37.             {  
  38.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 453.6);  
  39.             }  
  40.             if (btnLstFrom.Content.Equals("Ounces"))  
  41.             {  
  42.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 28);  
  43.             }  
  44.         }  
  45.         // Kilograms //  
  46.         if (lstTo.SelectedIndex.Equals(1))  
  47.         {  
  48.             btnLstTo.Content = "Kilograms";  
  49.             //txtLstTo.Text = "Millimeters";  
  50.             if (btnLstFrom.Content.Equals("Grams"))  
  51.             {  
  52.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.001);  
  53.             }  
  54.             if (btnLstFrom.Content.Equals("Kilograms"))  
  55.             {  
  56.                 txtResult.Text = txtFrom.Text;  
  57.             }  
  58.             if (btnLstFrom.Content.Equals("Metric tonnes"))  
  59.             {  
  60.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1000);  
  61.             }  
  62.             if (btnLstFrom.Content.Equals("Short ton"))  
  63.             {  
  64.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 907.2);  
  65.             }  
  66.             if (btnLstFrom.Content.Equals("Long ton"))  
  67.             {  
  68.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 1016);  
  69.             }  
  70.             if (btnLstFrom.Content.Equals("Pounds"))  
  71.             {  
  72.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.4536);  
  73.             }  
  74.             if (btnLstFrom.Content.Equals("Ounces"))  
  75.             {  
  76.                 txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.02835);  
  77.             }  
  78.         }  
  79.     }  
  80. }  
So when we change the value in the To ListBox the value of the Result Box will change depending on the ListBox value, such as shown below. Here we first select the Kilograms and then Ounces in the ListBox like this:

MassConvertor6.jpg

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 change depending on our From and To values like this:

MassConvertor7.jpg

Now we will change the value in the TextBox (45 to 41). The value of the ListBox will be changed:

MassConvertor8.jpg

Now we enter some value in the empty TextBox:

MassConvertor9.jpg

MassConvertor10.jpg

So now we write the code for that:

  1. txtError.Visibility = Visibility.Collapsed;  
  2.  if (txtFrom.Text == "")  
  3.  {  
  4.      txtFrom.Text = "";  
  5.      txtResult.Text = "";  
  6.  }  
  7.  else  
  8.  {  
  9.      // Grams //  
  10.      if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Grams"))))  
  11.      {  
  12.          if (txtFrom.Text.Contains('.'))  
  13.          {  
  14.              txtResult.Text = txtFrom.Text;  
  15.          }  
  16.          else  
  17.          {  
  18.              txtResult.Text = txtFrom.Text;  
  19.          }  
  20.      }  
  21.      if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Kilograms"))))  
  22.      {  
  23.              txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.001);  
  24.      }  
  25.      if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Metric tonnes"))))  
  26.      {  
  27.              txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.000001);  
  28.      }  
  29.      if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Short ton"))))  
  30.      {  
  31.              txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.000001);  
  32.      }  
  33.      if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Long ton"))))  
  34.      {  
  35.              txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 9.84e-07);  
  36.      }  
  37.      if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Pounds"))))  
  38.      {  
  39.              txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.002205);  
  40.      }  
  41.      if ((btnLstFrom.Content.Equals("Grams") && (btnLstTo.Content.Equals("Ounces"))))  
  42.      {  
  43.              txtResult.Text = Convert.ToString(Convert.ToDouble(txtFrom.Text) * 0.035273);  
  44.      }  


Similar Articles