Sunteți pe pagina 1din 9

Eastern Mediterranean University

School of Computing and Technology

ITEC319
Rapid Application
Development

Forms in Delphi
In Delphi, whenever a form is created in the project, two files
are created on the disk.
A .dfm file that creates a form file by deriving a class from
TForm.
A .pas file that creates a unit for the class definition.

The Delphi tool palette is used to select a component in order


to be placed on a form. Placing a component on a form is a
two-step process:
First, go to the tool palette and select the button representing
the component that should be placed on the form.
Then click onto the form to place the component on the form.
The component appears with its upper-left corner placed where
you clicked with the mouse.
2

1
Forms in Delphi
Multiple components of the same type can also be placed
onto the form without selecting the component from the
Component palette every time.
It can be done by pressing to the shift key on the keyboard
while selecting the component from the component palette.
After placing the components, mouse pointer (arrow) in the
tool palette window can be clicked to deselect the component.

There is also a shortcut to place a component onto the form.


If you double click onto the component button in the tool
palette window, that component will be placed to the center of
the form.

Working with Multiple Forms in Delphi


It is very usual that a Delphi project contains more than one
form.

Consider the following example that displays the second


form, when a button is clicked on the first form.
1. Create a new project.
2. Change the name of form1 to firstform and the caption to
Multiple Forms Test Program.
3. Select File->Save All by providing first as a unit file
name and multipleforms as a project file name.
4. Place a button on the form, change its name as showform2
and caption as Show Form 2.

2
Working with Multiple Forms in Delphi
5. Choose File->New Form from the main menu (or click the
New Form button on the toolbar) to create a new form.
6. Resize the new form so that it is about 50 percent of the size
of the main form.
7. Change the new form's name to secondform and caption to
Second Form.
8. Choose File->Save from the main menu and use second as a
unit file name.
9. Select the firstform and get into the onclick event of
showform2 button.
10. Type the following code:
procedure TMainForm.ShowFrom2Click(Sender: TObject);
begin
secondform.showmodal; // displays the second form on the screen
end;
5

Working with Multiple Forms in Delphi


When you run the program a message that says:
Form firstform references form secondform declared in
unit first which is not in your USES list. Do you wish to
add it?
Click Yes and let Delphi add the second unit name to the
uses list of the first unit.

In Delphi, in order to use a unit in another unit, its name


should be added to the uses list of the unit.
In this example, secondform is used within the firstform. So,
the name of the second unit should be added to the uses list of
the first unit.

3
Working with Multiple Forms in Delphi
In this example, when showmodal method is called for
secondform, it will be displayed on the screen and firstform
will not be accessible anymore until secondform is closed.
In Delphi, a modal form is one that must be dismissed before
the user can continue using the other forms in the application.
For example, most of the dialog boxes are modal.
To execute a modal form, showmodal method is called.

A modeless form is one that allows the user to continue to


work with the other forms of application.
To execute a modeless form, show method is called.

Dialog Boxes in Delphi


In general, dialog boxes that has a button usually labeled as
OK or Cancel or Close, are modal forms.

Dialog boxes can also have a Help button and usually they are
not usually re-sizeable.

In Delphi, you can create a dialog box just like creating an


ordinary form.

To create a dialog box in your program, the borderstyle


property of the form should be set as bsDialog.

4
Multiple Document Interface (MDI)

A single document interface (SDI) application has a single


main window and typically displays dialog boxes as needed,
but does not display child windows. When an SDI window is
minimized, its icon is placed on the Windows taskbar.

A multiple document interface (MDI) application consist of


a main window (MDI parent) and child windows (MDI
children). Child windows cannot be moved outside the parent
window. When an MDI child is minimized, its icon is
minimized within the MDI parent window.

Multiple Document Interface (MDI)

To create an MDI application in Delphi, the main form's


FormStyle property should be set to fsMDIForm.

On the other hand, each MDI child windows must have the
FormStyle property set to fsMDIChild.

An Example: Add three forms to your project, set one of


them as the parent window and the other two as the child
windows by changing the FormStyle property.

10

5
Form Properties (Design and Runtime)
The ActiveControl property is used to set the control that will have
focus when the form is activated.

If AutoScroll is set to True (the default), scrollbars automatically


appear when the form is too small to display all its components.

The BorderIcons property controls which system buttons will


appear on the form at runtime. Choices include the system menu,
the minimize button, the maximize button, and the help button.

The BorderStyle property indicates what type of border the form


will have. The default value is bsSizeable, which creates a window
that can be sized. Non-sizable styles include bsDialog and bsNone.

11

Form Properties (Design and Runtime)

You can specify the client area width and height rather than the full
form's width and height by using the ClientWidth and ClientHeight
properties. The client area of the form is the area inside of the
borders and below the title bar and menu bar. Setting these
properties makes automatic changes to the Width and Height
properties.

The Constraints property is used to set the maximum and


minimum width and height of the form. Simply set the MaxHeight,
MaxWidth, MinHeight, and MinWidth values as desired and the
form will conform to those constraints.

12

6
Form Properties (Design and Runtime)
The Font property specifies the font that the form uses. The
important issue to understand here is that the form's font is
inherited by any components placed on the form. This also means
that you can change the font used by all components at one time by
changing just the form's font.

FormStyle property is usually set to fsNormal. This property can


also be set to fsMDIForm for a parent MDI form, fsMDIChild for
a child MDI form and fsStayOnTop to keep the form always on top
of any other forms on the screen.

The Icon property sets the icon that is used on the title bar of a
form. If the BorderStyle property of a form is set to bsNone, then
the Icon property is ignored.

13

Form Properties (Design and Runtime)


When KeyPreview is set to True, the form's OnKeyPress and
OnKeyDown events will be triggered when a key is pressed in any
component on the form.

The Position property determines the size and position of the form
when the form is initially displayed on the screen.
poDesigned causes the form to be displayed in the exact
position that it was designed.
poDefault enables Windows to set the size and position of the
form.
poScreenCenter causes the form to be displayed in the center
of the screen.

14

7
Form Properties (Design and Runtime)

The Visible property controls whether the form is initially visible. It


can be used to hide or display the form.

The WindowState property is used to indicate how the form should


initially be displayed. It can be wsMinimized, wsMaximized and
wsNormal.

15

Form Properties (Runtime Only)

When read, the ActiveMDIChild property returns a pointer to the


currently active MDI child window. If no MDI child is currently
active or if the application is not an MDI application,
ActiveMDIChild returns nil.

The ClientRect property contains the top, left, right, and bottom
coordinates of the client area of the form.
For example, you might need to know the client area's width
and height in order to place a bitmap on the center of the form.

16

8
Form Properties (Runtime Only)

The Owner property is a pointer to the owner of the form. The


owner of the form is the object that is responsible for deleting the
form when the form is no longer needed. In the case of a main
form, the application object is both the owner of the form and the
parent of the form.

The Parent of a component, on the other hand, is the window (a


form or another component) that acts as the container for the
component. In the case of components, the owner would be the
form, but the parent could be another component such as a panel.

17

S-ar putea să vă placă și