004 – A new form creating and connecting it to main form of application

A new form creating and connecting it to main form of application


Contents





Task

We need to add the new form Form2 to main form Form1 of application.

In the main form Form1:

  • create the new button “Show Form – 2” for calling new form;
  • show the message in Form1 about the result of returning from Form2.

In the new Form2:

  • create two buttons with the names “OK” and “Cancel”;
  • program the reaction of form at the click of a mouse (keyboard) on the corresponding button.

The scheme of interaction between the forms is shown in figure 1.

01_02_00_004_01e

Fig. 1. The scheme interaction between the forms


Instructions

1. Run Delphi 2010. Creating the project as VCL Forms Application

A detailed example creating a new project in Delphi 2010 is described here. Save the project and the main form under name “MainForm” in some folder.

Automatically we have the name of main form “Form1” (property Name in Object Inspector).

 

2. Developing of main form Form1

Place on the main form the components TLabel and TButton.

Correspondingly the names of the components will be Label1 and Button1.

In the “Caption” property of Label1 component we type the text “Result = “. In the “Caption” property of Button1 component we type the text “Show Form 2”.

The main form of application has the view showed at figure 2.

01_02_00_004_02_

Figure. 2. Main form “Form1”

 

3. Adding the new form to project

The new form adds to project by calling of sequence of commands (figure 3). A new form is added to the project by calling a sequence of commands (fig. 3):

File -> New -> Form Delphi

01_02_00_004_03_

Fig. 3. Adding the new form into project

As a result, the new empty form will be displayed on the screen (fig. 4). To access to the properties and methods (procedures and functions) of this form we can use the name “Form2” by default.

01_02_00_004_04_

Fig. 4. Newly created form “Form2”

In Object Inspector the new form name is displayed in property “Name”. Optionally, you can change the name of the form.

To save the new form in file we need use command

File -> Save All

Each new form is saved in a separate file (module). By default, Delphi proposes name “Unit2.pas”. Let us leave this name.

Automatically, except module Unit2.pas, is created a file description of form Unit2.dfm.

Thus, we have two forms with the such names in program (property “Name”):

  • Form1 – main form (is placed in module “MainForm.pas”);
  • Form2 – secondary form (is placed in module “Unit2.pas”).

 

4. Setting up the new form view

To set the correct view of a new form (Form2) we execute such actions.

  1. We place on the form two buttons (component of TButton type) and one label (TLabel). We get the three variables-components with names: Button1, Button2, Label1.
  2. Property “Caption” of “Button1” component set to value “OK”.
  3. Property “Caption” of “Button2” component set to value “Cancel”.
  4. Property “Caption” of “Label1” component set to value “Form – 2”.

If necessary, we can set up and other properties of form “Form2”.

After changes and correcting the position of the components on the form “Form2” the new window of form will look like as shown in Fig. 5.

01_02_00_004_05_

Fig. 5. The view of secondary form after correcting

 

5. Connecting Form2 into module Form1

To get the result of returning from the form 2 (Form2) or get access to methods or properties of Form2 we need connect this form to the main form.

It is doing the directive “uses” at the beginning of text of the module of main form in section “implementation”.

...

implementation

uses Unit2;

...

end.

 

6. Calling the secondary form from the main form

To call secondary form (Form2) we program the event of clicking by mouse at the button “Show Form 2”.

Listing of program code of this event is the next.

procedure TForm1.Button1Click(Sender: TObject);
var
res:word;
begin
  // Calling the secondary form
  res := Form2.ShowModal; 
  if res=mrOk then
    Label1.Caption := 'Result = OK'
  else
    Label1.Caption := 'Result = Cancel';
end;

The immediate calling of secondary form is carried out by using ShowModal function, which returns one of two values (see. P. 7):

  • mrOk – it means that you clicked at “OK” button in the secondary form “Form2”;
  • mrNo – it means that you clicked at “Cancel” button in the secondary form “Form2”.

 

7. Programming the events in form2

The main task is to determine which button clicked in the secondary form Form2. From it depends the programs executing. For example, if you select the “OK” it may mean fulfillment of certain actions (read from the file, printing, etc.).

In our case we program two events in module of the form 2 (Unit2.pas):

  • mouse clicking at the button “OK” (Button1);
  • mouse clicking at the button “Cancel” (Button2);

Listing of code processing these of event has the following view.

procedure TForm2.Button1Click(Sender: TObject);
begin
  // closing form with return code mrOk
  ModalResult := mrOk; 
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  // Closing form with return code mrNo
  ModalResult := mrNo; 
end;

External global variable “ModalResult” of form Form2 determines the form behavior. As soon ModalResult is equal to non-zero value (mrOk or mrNo), the form is closed with an appropriate return code.

As result of calling the function from the main form

Form2.ShowModal

the secondary form will be displayed on screen for as long as the user will not close it by standard means of Windows.

If user selected the button “OK” then ShowModal will return “mrOk” value (see. p.6). In other case ShowModal will return “mrNo” value.

Now we can run the program and test it.


Conclusion

In this article was studied features of connection secondary form to the main form. Article includes the following themes.

  1. Creating a secondary form (File -> New Form).
  2. Connecting the secondary form to the main form of application (uses Unit2).
  3. Calling (displaying) the secondary form from the main form (ShowModal) and processing the result that were returned.
  4. Event handling processing of confirmation of work performed in secondary form by using a global variable ModalResult.


Related topics