Using two forms

To access one form from another you need to include the unit of the accessed form in the uses section of the other form. The next topic will have a demonstration of four forms linked together. The steps to link a main form to a password form are given below. We keep it simple, and do not encrypt the password (or make it difficult to guess). If you go wrong you can look at the essential code of five files after the instructions.

  1. Create a new application and save the unit as uPassword.pas and the project as Password.dpr.
  2. Open the form and add a button (tool-tip TButton) from the Standard tab of components and two labelled edits (tool-tip TLabeledEdit) from the Additional tab.
  3. Rename the components by changing in the Object Inspector the Name properties of the form, button and labelled edits to frmPassword, btnMain, leName and lePassword respectively.
  4. Using the Object Inspector, change the Caption property of the form and button to Password Form and Main Form, respectively.
  5. Using the Object Inspector for the first labelled edit, click on the + to the left of EditLabel then change the caption of the EditLabel to Name.
  6. Using the Object Inspector for the second labelled edit, change the PasswordChar property from #0 to *. Click on the + to the left of EditLabel then change the caption of the EditLabel to Password.
  7. Add a new form to the project using the menu item File > New > Form.
  8. Using the menu item File > Save As ..., save the unit of the new form as uMain.pas.
  9. Using the Object Inspector, change the name of the form to frmMain and its caption to Main Form.
  10. Add a label to form frmMain and change its name to lblMain
  11. At the beginning of the implementation section of the unit uMain.pas, add the code
    uses
      uPassword;
    
  12. At the beginning of the implementation section of the unit uPassword.pas, add the code
    uses
      uMain;
    
  13. View frmPassword and double click on the button. In its click event procedure insert the code:
    if lePassword.Text = 'pp4s' then
      frmMain.Show;  
    
  14. View frmMain and open the Object Inspector. Select the Events tab for the form, select the OnActivate event and double click in its empty edit box to be taken to the FormActivate procedure in the editor. Insert the code
    lblMain.Caption:= frmPassword.leName.Text + ', this is the main form.';   
    
  15. Resize the forms and position the components to your satisfaction.
  16. Run the program and enter 'Mo' for the name and 'pp4s' for the password. Click the button to see something like this screenshot.


    Both forms

The essential code for the files follows.

uPassword.pas

unit uPassword;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TfrmPassword = class(TForm)
    btnMain: TButton;
    leName: TLabeledEdit;
    lePassword: TLabeledEdit;
    procedure btnMainClick(Sender: TObject);
  end;

var
  frmPassword: TfrmPassword;

implementation
uses
  uMain;

{$R *.dfm}

procedure TfrmPassword.btnMainClick(Sender: TObject);
begin
  if lePassword.Text = 'pp4s' then
    frmMain.Show;
end;

end.

uPassword.dfm

object frmPassword: TfrmPassword
  Left = 193
  Top = 148
  Width = 200
  Height = 190
  Caption = 'Password Form'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 120
  TextHeight = 16
  object btnMain: TButton
    Left = 8
    Top = 104
    Width = 91
    Height = 25
    Caption = 'Main Form'
    TabOrder = 0
    OnClick = btnMainClick
  end
  object leName: TLabeledEdit
    Left = 8
    Top = 24
    Width = 121
    Height = 24
    EditLabel.Width = 37
    EditLabel.Height = 16
    EditLabel.Caption = 'Name'
    TabOrder = 1
  end
  object lePassword: TLabeledEdit
    Left = 8
    Top = 64
    Width = 121
    Height = 24
    EditLabel.Width = 60
    EditLabel.Height = 16
    EditLabel.Caption = 'Password'
    PasswordChar = '*'
    TabOrder = 2
  end
end
    

uMain.pas

unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TfrmMain = class(TForm)
    lblMain: TLabel;
    procedure FormActivate(Sender: TObject);
  end;

var
  frmMain: TfrmMain;

implementation

uses
  uPassword;

{$R *.dfm}

procedure TfrmMain.FormActivate(Sender: TObject);
begin
  lblMain.Caption:= frmPassword.leName.Text + ', this is the main form.';
end;

end.
      

uMain.dfm

object frmMain: TfrmMain
  Left = 192
  Top = 329
  Width = 200
  Height = 95
  Caption = 'Main Form'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnActivate = FormActivate
  PixelsPerInch = 120
  TextHeight = 16
  object lblMain: TLabel
    Left = 2
    Top = 8
    Width = 43
    Height = 16
    Caption = 'lblMain'
  end
end
      

Project file Password.dpr

program Password;

uses
  Forms,
  uPassword in 'uPassword.pas' {frmPassword},
  uMain in 'uMain.pas' {frmMain};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TfrmPassword, frmPassword);
  Application.CreateForm(TfrmMain, frmMain);
  Application.Run;
end.
Features introduced:
  • New form added to project

  • Other form opened

  • Data entered into one form accessed from another

  • FormActivate procedure used

Programming - a skill for life!

How to set up Lazarus to create, edit and run form-based Object-Pascal applications