Visual C# Learning



It is always a good thing to learn by real life examples.

In this tutorial we will learn a few more things about the Windows Form and in the next post we will write a program to calculate the area of a rectangle. We had already learned how to do this using the Command line in Console Application. We will now write the same thing in graphics GUI format.

We have shown the process of creating text boxes, labels and buttons in the following youtube. Basically, we have three text boxes, one each for length, width and area and three labels, just to the left of these three text boxes. We have a button, which on pressed will calculate and display the area.

Most of the things are straigtforward. The only thing that we wanted to point out was that, upon double clicking the button, you see the following code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           

        }
    }
}


We need to add the following code in the button1_Click

 private void button1_Click(object sender, EventArgs e)
        {
           
 area.Text =Convert.ToString(Convert.ToInt32(length.Text) * Convert.ToInt32(width.Text));

        }

Since the text are strings, we need to first convert them into integers before we can multiply them. Once they are multiplied, we again convert them to string using Convert.ToString. If you make any kind of error, you will get error if you try to run this program.