Delphi Programming for Beginners - Lesson 2

Form

TForm represents a window and it is an indirect successor of TComponent. If we create a form - it’s a successor of TForm. The whole description of form consists of components’ data and information about who owns which component. If we have a button on a form, it actually means that a form has one button among components related to this form. For example, if you move the button to the left, the Left property of that component changes. When saving a component (to a * .dfm file), a value of Left is saved (so if the current value is not the default value). During the compilation (or linking) phase, dfm files are inserted into the resulting binary (i.e. EXE file).

As a matter of fact, the * .dfm file contains a text description of the components related to a form and it is generated by the IDE. The form description, what is the content of the dfm file, can be displayed in the IDE with by pressing ALT + F12 when related form is shown. When compiled, the text description is converted to a binary one and attached to the application. When you create a form at run time, an instance of the form is created, and then all of the other components that belong to the form are created. When creating them, stored properties are loaded from the executable file.

Let’s look at a description of a form with TMemo component. The form has a name frmMain and a component called Editor. The form is the owner of Editor. This is the description of the form in fMain.dfm:


object frmMain: TfrmMain Left = 200 Top = 157 Width = 783 Height = 540 ActiveControl = Editor Caption = ‘Main window' Color = clBackground PixelsPerInch = 75 TextHeight = 16 TextWidth = 7 object Editor: TMemo Left = 0 Top = 0 Width = 783 Height = 540 Align = alClient TabOrder = 0 end end

and a class of the form in fMain.pas:


unit fMain;
interface
 type
  TfrmMain = class(TForm)
   Editor: TMemo;
  end;
 var
  frmMain:TfrmMain;
implementation
 {$R *.dfm} //we overlink dfm file 
end.

You may be now wondering who creates forms. If you look into dpr file (menu Project/View Source) you will see following:


program Test1;
uses
 Forms,
 Main in 'fMain.pas' {frmMain};
begin
 Application.Initialize; // initialization of object Application 
 Application.MainFormOnTaskbar := True;
 Application.CreateForm(TfrmMain, frmMain); // creation of a form 
 Application.Run; // we run it (launch it)
end.

In case of more forms a line with CreateForm is repeated (just with different parameters) for each form included in the project. If we don’t want to create all forms at launch (and we definitely don’t want to do that because more complicated the form is the more time it takes and it’s as well an unnecessary load for RAM), we can delete these lines. The other option is to tell IDE that we don’t want this (menu Project/Options/Forms). In that case we have to create form by ourselves when we need them. The first created form becomes the main form of the application so if you see a different form after the launch than you want to see, simply check the order of created forms and set proper one.

Some important properties of forms

Because a form (a window) is a thing a customer will see pretty often (if we code GUI application), it is a good idea to pay attention to certain details. Just so you know, GUI means Graphic User Interface, basically anything what can be seen on the screen and is used for communication with users.

Very important property is BorderStyle property, which sets the style of borders of a form:

  • bsSizeable – basic window, size can be changed
  • bsDialog – standard dialog window, size cannot be changed
  • bsNone – window with no borders (for instance, useful while creating splash screens)
  • bsSingle – size cannot be changed
  • bsSizeToolWin – smaller caption, size can be changed
  • bsToolWindows – smaller caption, size cannot be changed

Another important property is Caption - that is a caption of visible window. AlphaBlend and AlphaBlendValue – set whether the form is transparent and if so, how much.

BorderIcons set the visibility of system icons for maximization, minimization or system menu.

FormStyle set the style of a from: normal, fsMDIxxx (form is a part of MDI application), fsStayOnTop (always on the top).

Icon is an icon of a form. If it’s not used, the application icon is considered.

KeyPreview is a very interesting property. If it is set to True,all the pressed keys anywhere within the form will be processed by related Form's methods, not by certain Component's (where key has been pressed) only. This behavior could be very advantageous in many cases.

Position sets where a form will be shown (a centre of a monitor, ….). WindowsState sets how the form will be displayed (maximized, minimized, normal).

One of the details which can pretty easily upset the users is TabOrder. User normally expects that focus of components in a window will have a logical order. It’s quite common that user while repeatedly pressing TAB button expects it to switch among components in an order. TabOrder for the whole form is set via pop-menu above a form. If CnPack extension is installed, one of its function can be used for TabOrder setting in pretty comfortable way which is better than IDE-integrated way in my opinion.

Delphi Course

Show and ShowModal

These two form’s methods are used to display the form. The difference between them is that Show show the form and it returns immediately whereas ShowModal shows the form and it returns to the calling form once the displayed form is closed. ShowModal is used more often and it is used for selection from a dialog window - for example the selection of the name of a file.

Example

Lets have a project which has 2 forms - Form1 and Form2. Form1 is in the project right after the creation of a new application, the second form is added via menu New – Form.

Write “uses Unit2;” into uses section in implementation part of Form1 (Note: Form2 is usually saved in Unit2.pas).

We insert a button into Form1 and into button’s OnClick method we type “Form2.Show;” and then we insert another button and into its OnClick we type “Form2.ShowModal;”.

Now we insert a button with “OK” and another one with caption “Cancel” into Form2 and we want to know whether Form2 was closed by OK or Cancel. To do so we use button’s property ModalResult.

We set ModalResult on mrOK for OK button and mrCancel for Cancel button. We also set property Default for OK button (this button will be pressed whenever Enter is pressed) and property Cancel for Cancel button (and that means that pressing this button is basically the same as pressing ESC key).

Last thing we have to do is to change a code for a button with ShowModal in the first form to this code:


case Form2.ShowModal of
  mrOK:ShowMessage('OK'); 
  mrCancel:ShowMessage('Cancel');
end;

Homework

Change the previous program so that the second window’s size couldn’t be changed and that it will appear in the centre of a window. Then add TEditBox to Form1 and Form2 and change the code with ShowModal so that with pressing OK button the value of EditBox in Form2 will be copied to EditBox in Form1. In case of pressing Cancel button nothing should happen. Don’t forget about TabOrder!

Only if you're not sure, this is how you copy values: Edit1.Text := Form2.Edit1.Text;

Sample project regarding homework can be downloaded from here.

About the Author

Ing. Radek Červinka
Embarcadero MVP
Web: Delphi.cz
Twitter: @delphi.cz