Unit Converter Part: II (Speed Converter)

Before reading this article, please go through the following articles:

  1. Unit Converter: Part I (Mass Converter)

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

Meter/second, Meter/minute, Kilometer/hour, Foot/second, Foot/minute, Miles/hour

SpdCnv1.jpg

Step 1

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

SpdCnv2.jpg

Step 2

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

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

public class SpeedUnit
{
    public string SpeedUnits { get; set; }
}


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

public class SpeedList:List<SpeedUnit>

{

    public SpeedList()

    {

        Add(new SpeedUnit { SpeedUnits = "Meter/second" });

        Add(new SpeedUnit { SpeedUnits = "Meter/minute" });

        Add(new SpeedUnit { SpeedUnits = "Kilometer/hour " });

        Add(new SpeedUnit { SpeedUnits = "Foot/second" });

        Add(new SpeedUnit { SpeedUnits = "Foot/minute" });

        Add(new SpeedUnit { SpeedUnits = "Miles/hour" });
   }

}

Step 3

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

    {
        InitializeComponent();
        SpeedList();
    }

    private void SpeedList()
    {
        lstFrom.ItemsSource = new SpeedList();
        lstTo.ItemsSource = new SpeedList();
    }
}

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

        if (lstFrom.SelectedIndex.Equals(1))

        {

            btnLstFrom.Content = "Meter/minute";

        }

        if (lstFrom.SelectedIndex.Equals(2))

        {

            btnLstFrom.Content = "Kilometer/hour";

        }

        if (lstFrom.SelectedIndex.Equals(3))

        {

            btnLstFrom.Content = "Foot/second";

        }

        if (lstFrom.SelectedIndex.Equals(4))

        {

            btnLstFrom.Content = "Foot/minute";

        }

        if (lstFrom.SelectedIndex.Equals(5))

        {

            btnLstFrom.Content = "Miles/hour";

        }

 

        if (txtFrom.Text == "")

        {

            txtFrom.Text = "0";

        }

 

        if (txtTo.Text == "")

        {

            txtTo.Text = "1";

        }

 

        // Meter/second //

 

        if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Meter/second"))))

        {

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

            {

                txtTo.Text = txtFrom.Text;

            }

            else

            {

                txtTo.Text = txtFrom.Text;

            }

        }

        if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Meter/minute"))))

        {

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

            {

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

            }

            else

            {

                txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 59.988);

            }

        }

 

        if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Kilometer/hour"))))

        {

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

            {

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

            }

            else

            {

                txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 3.599712);

            }

        }

 

        if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Foot/second"))))

        {

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

            {

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

            }

            else

            {

                txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 3.28084);

            }

        }

 

        if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Foot/minute"))))

        {

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

            {

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

            }

            else

            {

                txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 196.8504);

            }

        }

 

        if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Miles/hour"))))

        {

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

            {

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

            }

            else

            {

                txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 2.237136);

            }

        }

 

}

Here we use only one example (Gram Units). (The complete code is available in the project). So we enter some value in the TextBox and click on the To ListBox and select some unit from there.

SpdCnv3.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 = "Meter/second";

 

             if (btnLstFrom.Content.Equals("Meter/second"))

             {

                 txtTo.Text = txtFrom.Text;

             }

             if (btnLstFrom.Content.Equals("Meter/minute"))

             {

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

                 {

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

                 }

                 else

                 {

                     txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 0.01667);

                 }

             }

             if (btnLstFrom.Content.Equals("Kilometer/hour"))

             {

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

                 {

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

                 }

                 else

                 {

                     txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 0.2778);

                 }

             }

             if (btnLstFrom.Content.Equals("Foot/second"))

             {

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

                 {

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

                 }

                 else

                 {

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

                 }

             }

             if (btnLstFrom.Content.Equals("Foot/minute"))

             {

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

                 {

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

                 }

                 else

                 {

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

                 }

             }

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

             {

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

                 {

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

                 }

                 else

                 {

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

                 }

             }
        } 

}

So when we change the value in the To ListBox the value of the Result Box will be changed according to the ListBox value like this. Here we first select the Foot/Minute and then Meter/Minute in the ListBox like this:

SpdCnv4.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 be changed according to our From and To values like this:

SpdCnv5.jpg

SpdCnv6.jpg

SpdCnv7.jpg
 

txtError.Visibility = Visibility.Collapsed;

            if (txtTo.Text == "")

            {

                txtTo.Text = "1";

            }

 

            // Meter/second //

 

            if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Meter/second"))))

            {

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

                {

                    txtTo.Text = txtFrom.Text;

                }

                else

                {

                    txtTo.Text = txtFrom.Text;

                }

            }

            if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Meter/minute"))))

            {

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

                {

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

                }

                else

                {

                    txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 59.988);

                }

            }

 

            if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Kilometer/hour"))))

            {

               

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

               

              

            }

 

            if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Foot/second"))))

            {

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

                {

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

                }

                else

                {

                    txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 3.28084);

                }

            }

 

            if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Foot/minute"))))

            {

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

                {

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

                }

                else

                {

                    txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 196.8504);

                }

            }

 

            if ((btnLstFrom.Content.Equals("Meter/second") && (btnLstTo.Content.Equals("Miles/hour"))))

            {

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

                {

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

                }

                else

                {

                    txtTo.Text = Convert.ToString(Convert.ToInt32(txtFrom.Text) * 2.237136);

                }

            }
 

Up Next
    Ebook Download
    View all
    Learn
    View all