C#. Windows Forms. An example of using a delegate to call an anonymous method. The development of the program for finding the area of a triangle using the Heron’s formula




An example of using a delegate to call an anonymous method. The development of the program for finding the area of a triangle using the Heron’s formula


Contents


Search other websites:

Task

Develop an application that finds the area of the triangle according to Heron’s formula. In an application, implement an anonymous method calling using a delegate. The method must calculate the area of the triangle. The application must be realized in Microsoft Visual Studio by the Windows Forms Application template.

 


Mathematical formulation of the problem

Heron’s formula looks like this:

where

  • S – area of a triangle;
  • a, b, c – lengths of sides of a triangle;
  • p – semiperperimeter, which is calculated by the formula:

 


Instructions

1. Creating a Project

Run the Microsoft Visual Studio. Create a project using the Windows Forms Application template. Save the project in any folder, for example:

D:\Programs\C_Sharp\TrainDelegates03

A more detailed example of creating and saving a project using the Windows Forms Application template is described in the topic:

As a result, a new application form will be created, as shown in Figure 1.

C#. Windows Forms Application template. Main form of application

Figure 1. The form of application after creation of project

 

2. Application form development

2.1. Placement of controls on the form

Place the following controls on the form:

  • 4 controls of type Label. As a result, 4 objects (variables) with the names label1, label2, label3, label4 will be created;
  • 1 control of Button type. An object named button1 will be created;
  • 3 controls of TextBox type. As a result, 3 objects will be created with the names textBox1, textBox2, textBox3.

After placing the controls, the form of the application will have an approximate look, as shown in Figure 2.

C#. Windows Forms Application template. The form of application after placement of controls

Figure 2. The form of application after placement of controls

 

2.2. Configuring controls

A more detailed example of how Button, Label, TextBox controls are configured is described in the topic:

Configure the following properties of the controls:

  • in the label1 control property Text = “a = “ (label1.Text = “a = “);
  • label2.Text = “b = “;
  • label3.Text = “c = “;
  • in the button1 control property Text = “Calculate” (button1.Text = “b = “);
  • in the object that corresponds to the Form1 form, property Text = “Area of a triangle”.

C#. Windows Forms template. The main form of application after configured

Figure 3. The main form of application after configured

 

3. Writing the program code

To write the code, go to the Form1.cs file, which corresponds to the main form of the application (program).

3.1. Declaring the delegate type

In the body of the Form1 class, declare the delegate type:

// declaring the delegate type
delegate float SquareTriangle(float a, float b, float c);

On the declared type of delegate it can be say:

  • the delegate type is named SquareTriangle;
  • a delegate of this type will receive three parameters of type float and return a value of type float.


After the type declaration, the text of the module “Form1.cs” is as follows:

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 TrainDelegates03
{
    public partial class Form1 : Form
    {
        // declaring the delegate type
        delegate float SquareTriangle(float a, float b, float c);

        public Form1()
        {
            InitializeComponent();
        }
    }
}

 

3.2. Programming the click event handler on the “Calculate” button

A detailed example of programming the click event on a button is described in the topic:

The text of the click event handler on the button button1:

private void button1_Click(object sender, EventArgs e)
{
    // Declaring a delegate with the name ST, which calculates the area of the triangle
    SquareTriangle ST;

    ST = delegate(float a, float b, float c)
    {
        float s, p, d;
        p = (a + b + c) / 2.0f;
        d = p * (p - a) * (p - b) * (p - c);
        if (d < 0) return -1.0f;
        s = (float)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
        return (float)s;
    };

    // get the values of lengths a, b, c
    float aa, bb, cc;
    aa = (float)Convert.ToDouble(textBox1.Text);
    bb = (float)Convert.ToDouble(textBox2.Text);
    cc = (float)Double.Parse(textBox3.Text); // It can also be

    // calling a delegate
    float area;
    area = (float)ST(aa, bb, cc);

    // display the result on the form
    label4.Text = "S = " + area.ToString();
}

Let’s explain some fragments of the code. In the event handler, a delegate with the ST name of the SquareTriangle type is declared. The ST delegate refers to an anonymous method that takes 3 parameters of type float. In the anonymous method, the area of the triangle is calculated by Heron’s formula. The result (area) is returned using the return statement.

The lengths of the sides that are entered from the keyboard (textBox1, textBox2, textBox3 controls) are placed in the variables aa, bb, cc. According to the C# syntax, variables that are declared in an anonymous method have visibility to the entire code block of the event handler button1_Click().Therefore, there can not be the same names in the event handler and the anonymous method that is described in this handler. That is, the declaration

float aa, bb, cc;

is true. If in the text of the event handler try to write

float a, b, c;

then there will be a compilation error, because such names are already used in the anonymous method.

 

3.3. Text of module Form1.cs

All program code of the module Form1.cs has the form:

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 TrainDelegates03
{
    public partial class Form1 : Form
    {
        // declaration of delegate type
        delegate float SquareTriangle(float a, float b, float c);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Declaring a delegate with the name ST, calculating the area of the triangle
            SquareTriangle ST;
            ST = delegate(float a, float b, float c)
            {
                float s, p, d;
                p = (a + b + c) / 2.0f;
                d = p * (p - a) * (p - b) * (p - c);
                if (d < 0) return -1.0f;
                s = (float)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
                return (float)s;
            };

            // get the values of the lengths a, b, c
            float aa, bb, cc;
            aa = (float)Convert.ToDouble(textBox1.Text);
            bb = (float)Convert.ToDouble(textBox2.Text);
            cc = (float)Double.Parse(textBox3.Text); // so too can

            // calling a delegate
            float area;
            area = (float)ST(aa, bb, cc);

            // displayin the result on the form
            label4.Text = "S = " + area.ToString();
        }
    }
}

 

4. Running the program for execution

After the performed actions, you can run the program for execution and test its work.

 


Related topics