Last Updated:

Event and Event Handling Function in C++

The appearance of the form you created suggests how the application works. Obviously, the user must enter the source data into the edit fields and click on the Calculate button. Clicking on a command button image is an example of what Windows calls an event.

An Event is something that happens while the program is running. In C++ Builder, each event is given a name. For example, a mouse click is an Onclick event, a double-click is an OnDb1Click event.

In Table. Figure 2.9 lists some Windows events.

Table 2.9. Events.

EventHappens
OnClickWhen you click the mouse button
OnDb1ClickWhen you double-click the mouse button
OnMouseDownWhen you click the mouse button
OnMouseUpWhen you release the mouse button
OnMouseMoveWhen you move the mouse
OnKeyPressWhen you press a keyboard key
OnKeyDownWhen you press the keyboard key. OnKeyDown and OnKeyPress events are alternating, repetitive events that occur until the held key is released (at which point the OnKeyUp event occurs)
OnKeyUpWhen you release a keyboard key
OnCreateWhen you create an object (form, control). the procedure for handling this event is typically used to initialize variables and perform preparatory actions
On PaintWhen a window appears on the screen at the beginning of the program; when the program is running after a window (or part of it) appears that has been closed by another window or minimized
OnEnterWhen the control receives focus
OnExitIf the control loses focus

The reaction to an event must be an action. In C++ Builder, event response is implemented as an event-handling function. Thus, in order for the program to perform some work in response to the user's actions, the programmer must write a function for handling the corresponding event. Note that a significant part of the event processing is handled by the component. Therefore, a programmer should develop an event-handling function only if the response to the event is different from the standard or undefined. For example, if, according to the condition of the task, there are no restrictions on the characters entered in the Edit field, then the procedure for handling the onKeypress event does not need to be written, because during the program's operation the standard (hidden from the programmer) procedure for handling this event will be used.

Let's consider the method of creating event-handling functions using the example of the onclick event handling function for the Calculate command button.

To start creating an event-handling function, you must first select the component for which you want to create an event-handling function. You can select a component in the Object Inspector window or by clicking on the component's image on the form. After that, in the Object Inspector window, select the Events tab.

The left column of the Events tab (Figure 2.18) lists the events that the selected component can perceive (the name and type of component are indicated at the top of the window). If a processing function is defined for the event, the name of the function appears next to the event name in the right column.

The Events tab lists the events that a component (in this case, a command button) can perceive.

In order to create an event handling function, you need to double-click the mouse in the Object Inspector window, in the field of the function for processing the corresponding event (Figure 2.19). This opens the Code Editor window and adds the Event Handling function template, and in the Object Inspector window, the C++ Builder generated name of the event-handling function (Figure 2.19) appears next to the event name.

Code Editor window

Fig. 2.19. Event-handling function template generated by C++ Builder

C++ Builder gives the event-handling function a name that consists of two parts. The first part of the name identifies the form that contains the object (component) for which the event-handling procedure was created. The second part of the name identifies the object itself and the event. In our example, the form name is Form1, the command button name is Button1, and the event name is Click.

In the Code Editor window, you can type statements between curly braces that implement the event-handling function.

Listing 2.1 shows the text of the onclick event handling function for the Calculate command button. Pay attention to how the program is presented. Its general appearance corresponds to how it looks in the Code Editor window: keywords are highlighted in bold, comments are in italics (selection is performed by the code editor). In addition, the instructions of the program are typed with indentation in accordance with the rules of good style adopted in the programming environment.

Code Listing 2.1. The simplest handling of the onclick event on the Calculate button.

void__fastcall TForml::ButtonlClick(TObject *Sender)
{
float u; // voltage
floatr; // resistance
float i; // current
// get data from input fields
u = StrToFloat(Editl › Text);
r = StrToFloat(Edit2 › Text);
// calculate current
i = u/r;
// display the result in the label field
Label4 › Caption = "Current: " +
FloatToStrF(i, ffGeneral, 7.2) + "A";
}

The Button1Click function performs a current calculation and displays the result of the calculation in the Label4 field. The source data is entered from the Edit1 and Edit2 edit fields by accessing the Text property. The Text property contains the string of characters that the user entered. For the program to work correctly, the user must enter an integer or fractional number in each edit field in the correct format (when entering a fractional number, a comma must be used to separate the integer and fractional parts).

Because the edit field contains text (the Text property of a string type), you must convert the string to a number. This task is solved by the strToFloat function, which passes the contents of the edit field as a parameter - the value of the Text property (Edit1 › Text is the content of the Edit1 field). The strToFloat function checks the characters of the string passed to it as a parameter for validity and, if all characters are correct, returns a value corresponding to the string received as a parameter.

After the initial data is placed in the variables u and r, the calculation is performed.

The calculated amperage is displayed in the Label4 field by assigning a value to the Caption property. To convert a number to a string of characters (the Caption property is a string type), use the FloatToStrF function.

Listing 2.2 shows how to handle the onclick event on the Finish command button. It is created in the same way as the onclick event procedure for the Calculate command button. As a result of clicking on the End button, the program should shut down. For this to happen, you need to close the program window. This is done by the close method.

Code Listing 2.2. The procedure for handling the Onclick event on the Finish button.

void __fastcall TForml::Button2Click(TObject*Sender)
{
Forml › Close();
}