Article submitted by: Kevin S. Gallagher
Welcome to the fourteenth chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
In just about every Delphi application, we use forms to present and retrieve information from users. Delphi arms us with a rich array of visual tools for creating forms and determining their properties and behavior. We can set them up at design time using the property editors and we can write code to re-set them dynamically at runtime.
In just about every Delphi application, we use forms to present and retrieve information from users. Delphi arms us with a rich array of visual tools for creating forms and determining their properties and behaviour. We can set them up at design time using the property editors and we can write code to re-set them dynamically at runtime.
Those of us who have used other visual programming languages such as Microsoft Visual Basic appreciate Delphi's many features for working with forms. However, even with all the creature comforts built into Delphi, there are many things we might want to do which cannot be solved by simply dropping a component onto a form and setting its properties. Over the next several months I will show you some of the simple and more complex tricks for making forms work more efficiently.
Starting Simply
To get things rolling, let's start off at the simple end with SDI (Single Document Interface) forms. An SDI application normally contains a single document view - as opposite to MDI (multiple document interface). By defualt, every time you create a new (Windows) application in Delphi, you start working in SDI application mode.
Starting a new Delphi project generates and displays the first form in the project file. To view the project file code, from the IDE menu go to Project | View Source.
The line we are interested in is painted red:
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
|
The method CreateForm prepares the form to be displayed as the program's main form. The form is not displayed until the next line executes:
Executing CreateForm utilizes memory and system resources. This is not a problem with an application with a single form, but if the project has many forms and they are all created at program startup you may run into low resource problems down the road. Your development computer probably has plenty of memory installed but the situation may be quite different on users' machines, where your application has to run with much more limited resources and in competition with other applications.
The solution is simple: take control of form creation and destruction.
Taking Control
Let's look at the basics for controlling forms in a new project. Imagine you have an application that has a main form (frmMain) with two subsidiary forms to add a new customer (frmAddNewCustomer - Unit2) and edit customers (frmEditCustomer - Unit3).
To add two new forms in the project, simply point to File | New | Form. This will create (and add) a new form named Form2 (+ Unit2) to the project. Rename this form (using the Object Inspector) to 'frmAddNewCustomer'. Repeat the steps for the third form, change the name to 'frmEditCustomer' (leave the name of the unit to be 'Unit3'). When you define the two subsidiary forms Delphi places the following code into the project file:
begin
Application.Initialize;
Application.CreateForm(TfrmMain, frmMain);
Application.CreateForm(TfrmAddNewCustomer, frmAddNewCustomer);
Application.CreateForm(TfrmEditCustomer, frmEditCustomer);
Application.Run;
end
|
Each Application.CreateForm... line creates the form object specified by the variable in the second argument as an instance of the class specified in the first. If you leave the code as it is, all three forms will be created in order. The first form in the list (named frmMain here, but it could be any name) is designated as the main form for the project and will be visible once Application.Run has executed. To show either of the other forms you would to apply code to some user-generated event to "show" it (i.e. bring it to the top):
frmAddNewCustomer.Show;
//or
frmAddNewCustomer.ShowModal;
|
Ok, so what are the diferences between the Show and ShowModal methods?
Next page > Displaying Modal forms > Page 1, 2, 3, 4
A Beginner's Guide to Delphi Programming: Next Chapter >>
>>
Communicating Between Forms