C++. C++ Builder. An example of creating and calling a new form from main form of application

An example of creating and calling a new form from main form of application in C++ Builder


The task

Develop the application, that calculates a volume of a prism. The formula of volume of prism is following:

05_01_00_008_formula_where V – volume of the prism, S0 – area of basis., h – the length of prism height.

The result must be outputted as a separate window. To get the window of result we need create a new form.


  1. Run Borland C++ Builder.

Create the new project as “VCL Form Application“. Save project in any folder.

The name of module of main form leave as is – “Unit1.cpp“. The project name leave as “Project1.cbproj“.


  1. Developing the main form of application.

At present, we have the main form of application named “Form1“. In Object Inspector you can change the form’s name with the help of property Name (Fig. 1). Leave all as is.


Fig. 1. Property “Name” of main form of application

From tab Standard you need to place on the form such components (Fig. 2):

  • component of type TButton;
  • two components of type TLabel;
  • two components of type TEdit.

We place components as shown in Figure 2.


Fig. 2. Placing components on the main form

As a result the objects (variables) are created with such names: Label1, Label2, Edit1, Edit2, Button1.

With the help of Object Inspector we need to set such properties of components:

  • in the component Label1 property Caption = “S = “;
  • in the component Label2 property Caption = “h = “;
  • in the component Edit1 property Text = “” (empty string);
  • in the component Edit2 property Text = “”;
  • in the component Button1 property Caption = “Calculate”.

Also, you need to change the title of form. To do this you need to select the form (component Form1) and in the property Caption enter the text “Volume of prism”.

After the changes and adjusting the size of components the application form has view as shown in Figure 3.


Fig. 3. Main form of application


  1. Creating the new form.

In the new form you can output the result of calculation. The new form will be called after pressing on the button “Calculate”.

To create a new form you need choose following command (Fig. 4):

File -> New -> Form C++ Builder


Fig. 4. Command of creating the new form

As a result, the window of newly created form will be showed. The object (variable) named “Form2” corresponds to the new form (Fig. 5).


Fig. 5. Newly created form

After calling the command of saving

File -> Save All

will be proposed to save the file of module of form as “Unit2.cpp“. Let’s leave it as is.

Moreover, the file “Unit2.dfm” will be created. In this file is described information about settings of form: form size, background color, text color, font settings and so on.


  1. Creating a new form Form2.

We place on the form Form2 such components:

– component of type TLabel;

– component of type TButton.

As a result, two objects (variables) with names “Label1” and “Button1” will be created.

Also, you need correct the size of form using mouse.

As a result, the form Form2 will have view as shown in Figure 6.


Fig. 6. The newly created form with placement of components

Let’s set up some properties of form Form2. To do this, first, we need to select form Form2.

The next step is set such properties of form Form2 using “Object Inspector”:

– property Caption = “Result“;

– property BorderStyle = “bsDialog”. After that, form will look like as a dialog window.

Additionally, we make setting components Label1 and Button1 of form Form2:

  • in component Label1 property Caption = “Result = “;
  • in component Button1 property Caption = “OK”.

As a result, form will have view as shown in Figure 7.


Fig. 7. Newly created form after settings


  1. Programming an event of click on the button “OK” of form Form2.

To get the result of returning from form Form2 and close the window of form, you need to call an event of click by mouse on the button “OK”.

To do this you need to do such sequence of actions (Fig. 8):

– select the button Button1 of form Form2;

– in Object Inspector go to tab “Events”;

– in the list of events find the event OnClick;

– make the double click by mouse in text field opposite the event OnClick.


Fig. 8. Calling the event OnClick

As a result, the code of procedure of event handling will be open:

void __fastcall TForm2::Button1Click(TObject *Sender)


Between the braces { } we need to write code of event handler. Enter the following string of code:

ModalResult = mrOk;

Thus, the procedure of event handling will have view:

void __fastcall TForm2::Button1Click(TObject *Sender)
     ModalResult = mrOk;   

In the code snippet is used a global variable of form named as ModalResult. This variable is responsible for the behavior of the form window. By default the value of variable is zero. It means, that the window of form is closed. If value of variable ModalResult becomes nonzero, then the form is closing with the returning code of this value.

In our case, in the variable ModalResult is set the value of global constant mrOk. It means, that the window of form “Form2” will be closed, with the returning code mrOk.

The global constant ModalResult can taking and other values, for example mrAll, mrCancel, mrIgnore, mrNo, mrYes, mrRetry and so on.


  1. Setting the connection between forms.

To call the form “Form2” from form “Form1” you need to connect the module “Unit2.cpp” in file “Unit1.cpp” with the module of main form Form1. File “Unit2.cpp” corresponds to form Form2.

It is realized by standard way for C++. In the header file “Unit1.h” of form Form1 we type the text of connecting of module “Unit2.h“:

#include "Unit2.h"

To go to the code of module “Unit1.h” we need to do the double mouse click at the name “Unit1.h” in Project Manager (see Fig. 9).


Fig. 9. The connection string of module “Unit2.h” to module “Unit1.h


  1. Programming of calculation of prism volume.

To calculate the volume of prism you need to program the event of clicking on the button “Calculate” of form Form1.

To do it we do following operations:

– with the help of Project Manager we need to go to the form “Form1” by selecting file “Unit1.dfm” (Fig. 10);

– call the code of event handling of click on the button “Calculate” (see p. 5). Calling of code of handling event is realized the same as in paragraph 5;

– type the text of calculation of volume of sphere.


Fig. 10. The selection of file “Unit1.dfm” in Project Manager

Listing of event handler of clicking on the button “Calculate” is following.

void __fastcall TForm1::Button1Click(TObject *Sender)
     float s, h, v;
     s = StrToFloat(Edit1->Text); // get the square
     h = StrToFloat(Edit2->Text); // get the height of the prism
     v = s * h; // calculation of volume
     Form2->Label1->Caption = "Result = " + FloatToStrF(v,ffFixed, 8,2);
     Form2->ShowModal(); // show the result form

In the code snippet to convert from string type to floating point type we use function StrToFloat.

To convert back from floating type to string we use function FloatToStrF. This function gets input data of corresponding floating point value v, type of output format (ffFixed), the width of the output field (8 characters), precision of output result (2 decimal places).

Now, you can run the project and test it.