005 – Example of event programming of button click in C#

Example of event programming of button click in C#.  Development of a program to determine the surface area of the sphere

Problem condition

Develop the program, which finds the area of sphere surface based on radius R. Spheres surface is calculated by formula

02_02_00_005_00_

where R – radius of sphere, π – constant, that equals 3.1415.

In program we need to use following controls:

  • Label – for messages;
  • Button – button of beginning calculation;
  • TextBox – text box, that used for input of value R.

 

Instructions

  1. Running MS Visual Studio 2010.

Saving the project.

 

  1. Controls Label, Button, TextBox.

 From “CommonControls” tab we place on form four controls (Fig. 1):

– two controls of Label type;

– the control of Button type;

– the control of TextBox type (input string).

02_02_00_005_01_

Fig. 1. Controls Label, Button, TextBox

  Automatically, four objects with names “label1“, “label2“, “button1“, “textBox1” will be created.

  By these names we will be able to access properties of these objects.

  Program’s form will have the following view (Fig. 2).

02_02_00_005_02_

Fig. 2. Program’s form after placing label1, label2, button1, textBox1

 

  1. Setting up controls of Label type.

  Select the label1 control. In the Toolbox palette change the Text property – enter “R = “.

  Similarly, change the Text property for the label2 control (Fig. 3). Input text “The area of sphere surface = “.

02_02_00_005_03e

Fig. 3. Changing the “Text” property to label1

 

  1. Control “Button“.

   Similarly to label1 control (object) we select button1 control. In the “Text” property enter string “Calculate“.

 

  1. Editing of form view.

  Change the title of form. To do this we select the form. In the “Text” property of form we type text “The area of sphere surface = “.

  After editing, the form view will be following (Fig. 4).

02_02_00_005_04e

Fig. 4. The view of form after editing

  1. Programming of event handler of click on the “Calculate” button.

6.1. Calling of program code.

  In our program we need to handle the event, which is generated when user clicks on the button “Calculate“.

  After clicking on button “Calculate” the snippet of program code will be formed. This code will be processed by our application. In time of handling, first of all, the value of radius R is defined. After that, we do the calculation by formula and we do output of result.

To call the snippet of event handle of clicking on the button button1, we need to do following:

– select “button1” button (Fig. 5 – 1);

– go to the “Event” tab (events) in window of properties (Fig. 5 – 2);

– do the double click by “mouse” in field of “Click” event (fig. 5 – 3).

02_02_00_005_05e

Fig. 5. The sequence of call of code snippet of the event handler

  As a result the tab of program code, which is placed in to file “Form1.cs“, will be opened (Fig. 6).

02_02_00_005_06_

Fig. 6. Method of event handle of clicking on the button “button1“.

  Listing of program code is the next:

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 button1_Click(object sender, EventArgs e)
    {

    }
  }
}

 

6.2. Inputting of program code of event handler

   In method button1_Click type the code of event handler. Listing of this method is the next:

 

...
private void button1_Click(object sender, EventArgs e)
{
  const double Pi = 3.1415;
  double R,S;
  R = Double.Parse(textBox1.Text);
  S = 4 * Pi * R * R;
  label2.Text = "The area of sphere surface = " + S.ToString();
}
...

  Two variables R and S of double type are defined in this code. Also, the constant Pi is described.

  To converting from text type “string” to “double” type, is used the “Parse” method. So the value of variable R is filled.

R = Double.Parse(textBox1.Text.ToString());

  Likewise, we can convert data other types. For example, we can write following code for “int” type:

int d;
d = Int32.Parse("100"); // d = 100

  The result of calculating of area of sphere surface is displayed in label2.Text property.

  Converting to “string” type is realized with the help of ToString() method.

 

6.3. Editing the program code.

  The code, described in paragraph 6.2, does not have protection against incorrect input of value R. Therefore button1_Click method should be rewritten as follows:

...
private void button1_Click(object sender, EventArgs e)
{
  try
  {
    const double Pi = 3.1415;
    double R, S;
    R = Double.Parse(textBox1.Text);
    S = 4 * Pi * R * R;
    label2.Text = "The area of sphere surface = " + S.ToString();
  }
  catch (FormatException ex)
  {
    label2.Text = "Error - " + ex.Message;
  }
}
...

Code snippet

try
{
  ...
}
catch (...)
{
  ...
}

allows program to intercept a critical situation, which can be generated as a result of incorrect input to the “textBox1” field (for example “abcdefg“).

    In this case the message

Error – Input string was not in a correct format

will be displayed in ex.Message property.

 

  1. Executing program.

   After that you can run our program and test it operation at any value of R.