Temperature Converter in C#

Temperature Converter

This program was developed in C# programming Language, in this article will be presented the code source of the application and the steps to develop it.

This Desktop application was develop at Visual Studio 2012 (there are other Version Available).

This Application work as a temperature convertor from Celsius to Fahrenheit and vice-versa. To do the conversion we need to know the formulas to do such conversions.

To convert from Celsius to Fahrenheit:

F = (C * 9) / 5 + 32;

To convert from Fahrenheit to Celsius:

C = (F – 32) * 5 / 9;

Where F = Fahrenheit and C = Celsius.

To build this Converter were used 2 group-boxes where one contains 2 radio-buttons that are used to choose the conversion options, and the other one contains 2 labels to identify the text-boxes there used. There is also a picture-box that holds our image there (it is optional).

Note:

There was no need to have a button, due to the fact that I have chosen that the values will change when a number entered.

Let’s code:

After designing the form, we are going to get our hands dirty with code LOL…

Step 1: click F7 on your keyboard and it will direct you to the source code, where you have to declare the following variables:

  1. int cel = 0, fr = 0; //names of the variable can variate according to you  
  2. // cel for Celsius and fr for Fahrenheit  
Step 2:

After declaring those variables, double click on your Form in an empty space in order to specify some actions that will occur when Form loaded, in this case we want to have one of those radio-buttons checked already when application loaded:
  1. private void Form1_Load(object sender, EventArgs e)  
  2.         {  
  3.             rbnCF.Checked = true;  
  4.         }  
Step 3:

After defining which radio-button is checked when application is loaded, is defined which actions happens when one of the two radio button is checked. It going to define which text-box will be enable to user enter data. You have to double-click at the radio-buttons one at a time.
  1. // first radio-button  
  2. private void rbnFC_CheckedChanged(object sender, EventArgs e)  
  3.         {  
  4.             if (rbnFC.Checked == true)  
  5.             {     
  6.                 txtFahrenheit.Enabled = true;  
  7.                 txtCelsious.Enabled = false;  
  8.             }  
  9.         }  
  10.   
  11. //second radio-button  
  12.         private void rbnCF_CheckedChanged(object sender, EventArgs e)  
  13.         {  
  14.             if (rbnCF.Checked == true)  
  15.             {  
  16.                 txtFahrenheit.Enabled = false;  
  17.                 txtCelsious.Enabled = true;  
  18.             }  
  19.         }  
Step 4:

Now it is time to do our calculations, the text-boxes were coded as it produces an output result each time the input changes, for this we use the TextChanged event on both text-boxes.
  1. // textbox Celsius  
  2. private void txtCelsious_TextChanged(object sender, EventArgs e)  
  3.         {  
  4.             if (txtCelsious.Text != "" && rbnCF.Checked==true)  
  5.             {  
  6.                 cel = int.Parse(txtCelsious.Text);  
  7.                 fr = (cel * 9) / 5 + 32;  
  8.   
  9.                 txtFahrenheit.Text = fr.ToString();  
  10.             }  
  11.               
  12.         }  
  13.   
  14. //textbox Fahrenheit   
  15.         private void txtFahrenheit_TextChanged(object sender, EventArgs e)  
  16.         {  
  17.             if (txtFahrenheit.Text != "" && rbnFC.Checked==true)  
  18.             {  
  19.                 fr = int.Parse(txtFahrenheit.Text);  
  20.                 cel = (fr - 32) * 5 / 9;  
  21.   
  22.                 txtCelsious.Text = cel.ToString();  
  23.             }  
  24.         }  
Now you can try it yourself, be creative and make your changes. Code source will be available

Author: Bruno Kiafuka.

 

Ebook Download
View all
Learn
View all