Windows Programming Tutorial

by Dr. Zhang

Examples: Adder, Temperature Converter and Vacation Planner

  1. Start MS Visual Studio by clicking Start menu, then click All Programs -> Microsoft Visual Studio 2010.
  2. Click File menu and select New -> Project
    You should see a New Project window:

  3. In the left panel Project types, expand Visual C# and select Windows
  4. In the right panel Templates, select Windows Forms Application
  5. In the text box Name, type a name for the project
  6. In the text box Location, select a directory (folder) you want to save this project
  7. Click OK
    You should see a design window with an empty form:

  8. In the Toolbox, drag a Label and a Textbox to the form. If the Toolbox doesn't show up, click View menu and select Toolbox.

  9. Select (click once) the Label in the form, in the Properties Window, type a name as the value of the Text field for the Label.
  10. Repeat the same for other labels, textboxes, and button.
  11. Add a button with (Name) called "Add"

  12. Double click the button (gently and only once), insert an "event handler" program in a code window. Note that the names of the graphical elements must match the names in the code.
    	private void button1_Click(object sender, EventArgs e)
    		double sum;
    		sum = Convert.ToDouble(textBox1.Text) + Convert.ToDouble(textBox2.Text);
    		label1.Text = sum.ToString();
  13. Click Build menu > Build Solution
  14. Click Debug menu > Start without debugging.

  15. Test program.
  16. Well done with your first Windows software product.

Example: Temperature Converter

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TemperatureConverterVS2012
    public partial class Form1 : Form
        public Form1()

        //Convert from Fahrenheit to Celsius
        private void button1_Click(object sender, EventArgs e)
            double tempC = 0;
            double tempF = 0;

            tempF = Convert.ToDouble(textBox1.Text);
            tempC = 5.0 / 9.0 * (tempF - 32);

            textBox2.Text = tempC.ToString("0.00");

        //Convert from Celsius to Fahrenheit
        private void button2_Click(object sender, EventArgs e)
            double tempC = 0;
            double tempF = 0;

            tempC = Convert.ToDouble(textBox2.Text);
            tempF = 9.0 / 5.0 * tempC + 32;

            textBox1.Text = tempF.ToString("0.00");

        private void textBox1_TextChanged(object sender, EventArgs e)
            //button1_Click(sender, e);

        private void textBox2_TextChanged(object sender, EventArgs e)
            //button2_Click(sender, e);

Example: Vacation Planner

In this example, more graphical controls are used, such as calendars, dropdown menus and radio buttons.

Tips of Form Design:

  1. Use MonthCalendar control from toolbox for calendars
  2. Use ComboBox control from toolbox for dropdown menus
  3. Use Items in the Properties Window to add choices for a ComboBox. Note that the first choice (option) in the menu corresponds to integer value of zero.
  4. Use RadioButton control from toolbox for radio buttons
  5. Use GroupBox control under Containers category from toolbox to group radio buttons so that the radio buttons inside the GroupBox are cohesive as a single "family" and have mutual exclusive behavior, that is, only one radio button is selected at a time.
  6. Use PictureBox control from toolbox for pictures.
  7. Use Label control from toolbox to display calculation results.

Code Behind in C#

        public Form1()

            //Set the number of rooms to zero and the first city selected
            comboBox1.SelectedIndex = 0;
            radioButton1.Checked = true;

        private void button1_Click(object sender, EventArgs e)
            //Declarations of variables
            int rooms;
            int nights;
            double rate;
            double total;
            DateTime checkin;
            DateTime checkout;
            TimeSpan duration;

            //Get the number of rooms booked
            rooms = comboBox1.SelectedIndex;

            //Get arrival and departure dates
            checkin = monthCalendar1.SelectionStart;
            checkout = monthCalendar2.SelectionStart;

            //Calculate the number of days to stay in
            duration = checkout - checkin;
            nights = duration.Days;

            if (nights <= 0)
                MessageBox.Show("Departure date should be later than arrival date");

            //Determine the room rates based on the cities
            if (radioButton1.Checked == true)
                rate = 200.0;
            else if (radioButton2.Checked == true)
                rate = 150.0;
                rate = 250.0;

            //Calculate the total costs
            total = rate * rooms * nights;

            //Display the total costs in the text box
            label6.Text = "$" + total.ToString();