Temperature converting

In this post I'll try to explain you how to convert temperatures from Celsius to Fahrenheit or vice versa. I will create a small WinForms application that actually does that.

I used two doubles to store the temperature's values (one for Celsius and one for Fahrenheit), two labels (of which one will be displayed after the actual transformation takes places and will contain the transformed value), one combobox and one textbox. Oh... and almost forgot about the button that triggers the transformation and displays the result.

One more thing you will notice is that I've used Math.Round with two decimals to get a nice transformation result and I've used the String.Format method to display the transformed value instead of the usual concatenation.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TemperatureTrasformer
{
    public partial class mainForm : Form
    {
        double celsius;
        double fahrenheit;

        ComboBox cboxTempType;
        TextBox txtTemp;
        Label lblInitial;
        Label lblResult;
        Button btnConvert;

        public mainForm()
        {
            InitializeComponent();

            //Set up the form's proprieties
            this.Text = "Temperature Converter";
            this.ClientSize = new Size(250, 150);
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

            //Set up the form's controls
            lblInitial = new Label();
            lblInitial.Location = new Point(10, 10);
            lblInitial.AutoSize = true;
            lblInitial.Text = "Enter the temperature you want to convert:";

            txtTemp = new TextBox();
            txtTemp.Location = new Point(10, lblInitial.Bottom + 5); 
            txtTemp.Width = 100;

            cboxTempType = new ComboBox();
            cboxTempType.Location = new Point(txtTemp.Right + 10, lblInitial.Bottom + 5);
            cboxTempType.Width = 100;
            cboxTempType.DropDownStyle = ComboBoxStyle.DropDownList;
            cboxTempType.Items.Add("Celsius");
            cboxTempType.Items.Add("Fahrenheit");
            cboxTempType.SelectedIndex = 0;

            btnConvert = new Button();
            btnConvert.Text = "Convert";
            btnConvert.Location = new Point(10, txtTemp.Bottom + 10);
            btnConvert.AutoSize = true;
            btnConvert.Click += new EventHandler(btnConvert_Click);

            lblResult = new Label();
            lblResult.Location = new Point(10, btnConvert.Bottom + 10);
            lblResult.AutoSize = true;

            this.Controls.Add(lblInitial);
            this.Controls.Add(cboxTempType);
            this.Controls.Add(txtTemp);
            this.Controls.Add(btnConvert);
        }

        void btnConvert_Click(object sender, EventArgs e)
        {
            //Convert to Fahrenheit
            if (cboxTempType.SelectedIndex == 0)
            {
                celsius = Convert.ToDouble(txtTemp.Text);
                fahrenheit = Math.Round((celsius * 9) / 5 + 32, 2);
                lblResult.Text = String.Format("The temperature in Fahrenheit is {0} degrees", fahrenheit);
                this.Controls.Add(lblResult);
            }
            //Convert to Celsius
            else if (cboxTempType.SelectedIndex == 1)
            {
                fahrenheit = Convert.ToDouble(txtTemp.Text);
                celsius = Math.Round((fahrenheit - 32) / 9 * 5, 2);
                lblResult.Text = String.Format("The temperature in Celsius is {0} degrees", celsius);
                this.Controls.Add(lblResult);
            }
        }
    }
}