C++. An example of event programming in C++ Builder




An example of event programming in C++ Builder

In this article the example of event programming of clicking at the button with displaying of corresponding message is described.

 

The task

Create a simple program to count the number of clicks on the button of TButton type. Current result must be displayed in TLabel component.

Progress

  1. Run Code Gear C++ Builder.

Create the project as VCL Forms Application. Save the project.

  1. Placement of TButton component at the form.

Component TButton is placed at Standard tab (fig. 1).

05_01_00_006_01_

Fig. 1. Component TButton from tool palette “Tool Palette

From Standard tab of tool palette “Tool Palette” we need to place the TButton component at the form of application (fig. 2).

Automatically object (variable) named Button1 will be created (property “Name“).

05_01_00_006_02_

Fig. 2. The TButton component at the form of application

 

  1. Placing of TLabel component on the form.

In palette “Tool Palette” we find component TLabel (a label), which is the text to output on the form (fig. 3).

05_01_00_006_03_

Fig. 3. The TLabel component at the tool palette

Place on the form TLabel component. The new object (variable) named Label1 is created.

Edit the form size and components TLabel and TButton thus (approximately) as displayed at the figure 4.

05_01_00_006_04_

Fig. 4. Editing of application view

  1. Setting up of properties of TForm, TLabel and TButton components.

Change the form name. For this:

– select the form Form1 (with the help of “mouse”);

– in Object Inspector in “Caption” property set the value “Counting the number of clicks“.

Change the text in Label1. For this:

– select the component Label1;

– in Object Inspector in “Caption” property enter the text “Number of clicks = “.

Change the text in Button1. For this:

– select the Button1 component;

– in Object Inspector in “Caption” property enter the text “”.

The form of the application after the settings will look as shown in Fig. 5.

05_01_00_006_05e

Fig. 5. The form view after setting up.

 

  1. Programming the event of click on the button.

Windows provides a huge set events that can be processed by software. One of the most commonly used events is a click event on the button. This event is named OnClick. The names of all events begin from prefix “On“. In our case we program the “OnClick” event of Button1 component.

For this you must first select the component Button1 and select the tab “Events” in the Object Inspector. In the list of available events find the “OnClick” event (fig. 6).

05_01_00_006_06e

Fig. 6. The “OnClick” event of Button1 component

After that we need to call the program code of handling procedures of this event.

For this there are two methods:

– double click by “mouse” in area of OnClick string in Object Inspector;

– double click by “mouse” in area of component (button) Button1 on the form (only for TButton).

As a result of selecting one of two methods window of code editor will be opened. Code listing is the next.

void __fastcall TForm1::Button1Click(TObject *Sender)
{

}

Between braces need to fit our event-handling code. We inscribe our code in item 9 of this article. At first we describe the internal variable.

 

  1. Adding the variable-counter.

According to the statement of the problem, you need to keep a record of the number of clicks “mouse” at the button Button1.

To do this, enter the code a variable k in the description of the class TForm1.

Class TForm1 automatically were created by Borland C++ Builder. This class corresponds to the main application form Form1.

Class description is in module “Unit1.h“. Unit1 – file name of the module that was specified when you save the project. Transition to the module (file) “Unit1.h” displayed in Fig. 7.

05_01_00_006_07_

Fig. 7. Module Unit1.h

Program’s listing is the following.

//-----------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//-----------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//-----------------------------------------------------------
class TForm1 : public TForm
{
  __published:      // IDE-managed Components
  TButton *Button1;
  TLabel *Label1;
  void __fastcall Button1Click(TObject *Sender);
  private:      // User declarations
  public:       // User declarations
  __fastcall TForm1(TComponent* Owner);
};
//------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//------------------------------------------------------
#endif

Between the words “private:” and “public:” we write the variable k of integer type. The code snippet is as follows:

...
private:      // User declarations
  int k;
public:           // User declarations
...

In the above listing variable k is described in form’s class as private variable (section private).






  1. Programming the event OnActivate of form activation.

To reset a variable, we need to program “OnActivate” event of form activation. This event is generated by system

This event is generated by the system when you start application. It is used for initialization values of internal form variables.

Select the form, choose the “Events” tab. At the figure 8 is displayed the placement of event on the “Events” tab in Object Inspector.

05_01_00_006_08_

Fig. 8. The event OnActivate of form Form1

Call the listing of procedure of OnActivate event handling by double clicking into the event’s name string. The listing is following.

 

void __fastcall TForm1::FormActivate(TObject *Sender)
{
  // resetting the counter
  k = 0;
  // show message
  Label1->Caption = "Number of clicks = " + IntToStr(k);
}

 

  1. Programming the event’s handling code of clicking at the button Button1.

Call the program code of OnClick event of Button1 component (see p. 6). We form the code listing as follows.

 

void __fastcall TForm1::Button1Click(TObject *Sender)
{
     k++;
     Label1->Caption = "Number of clicks = " + IntToStr(k);
}

 

In the above listing the value of k variable is increased onto 1 everytime when user will press at the button Button1.

Function IntToStr (k) translates numerical value of k into string value for representing it on the form of application.

After, we can run project.