Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 6

Chapter 06 - Project QInFocus

Project Structure

QInFocus.dpr
program QInFocus;

uses
  QForms,
  FocusF in 'FocusF.pas' {FocusForm};

{$R *.res}

begin
  Application.CreateForm(TFocusForm, FocusForm);
  Application.Run;
end.
FocusF.pas
unit FocusF;

interface

uses
  SysUtils, Qt, Classes, QGraphics, QControls,
  QForms, QDialogs, QStdCtrls, QComCtrls, QExtCtrls;

type
  TFocusForm = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    EditFirstName: TEdit;
    EditLastName: TEdit;
    EditPassword: TEdit;
    StatusBar1: TStatusBar;
    procedure GlobalEnter(Sender: TObject);
    procedure EditFirstNameExit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FocusForm: TFocusForm;

implementation

{$R *.xfm}

procedure TFocusForm.GlobalEnter(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to ControlCount - 1 do
    // if the control is a label
    if (Controls [I] is TLabel) and
      // and the label is connected to the current edit box
      (TLabel(Controls[I]).FocusControl = Sender) then
    // copy the text leaving off the initial & character
    StatusBar1.SimpleText := 'Enter ' +
      Copy (TLabel(Controls[I]).Caption, 2, 1000);
end;

procedure TFocusForm.EditFirstNameExit(Sender: TObject);
begin
  if EditFirstName.Text = '' then
  begin
    // don't let the user get out
    EditFirstName.SetFocus;
    MessageDlg ('First name is required',
      mtError, [mbOK], 0);
  end
  else if EditFirstName.Text = 'Admin' then
  begin
    // fill the second edit and jump to the third
    EditLastName.Text := 'Admin';
    EditPassword.SetFocus;
  end;
end;

end.
FocusF.xfm
object FocusForm: TFocusForm
  Left = 252
  Top = 92
  Width = 308
  Height = 189
  VertScrollBar.Range = 140
  HorzScrollBar.Range = 273
  ActiveControl = EditFirstName
  Caption = 'InFocus'
  Color = clButton
  Font.Color = clBlack
  Font.Height = 11
  Font.Name = 'MS Sans Serif'
  Font.Pitch = fpVariable
  Font.Style = []
  Font.Weight = 40
  ParentFont = False
  PixelsPerInch = 96
  TextHeight = 13
  TextWidth = 6
  object Label1: TLabel
    Left = 16
    Top = 24
    Width = 48
    Height = 13
    Caption = '&First name'
    FocusControl = EditFirstName
  end
  object Label2: TLabel
    Left = 16
    Top = 64
    Width = 49
    Height = 13
    Caption = '&Last name'
    FocusControl = EditLastName
  end
  object Label3: TLabel
    Left = 16
    Top = 104
    Width = 46
    Height = 13
    Caption = '&Password'
    FocusControl = EditPassword
  end
  object EditFirstName: TEdit
    Left = 84
    Top = 20
    Width = 189
    Height = 21
    TabOrder = 0
    OnEnter = GlobalEnter
    OnExit = EditFirstNameExit
  end
  object EditLastName: TEdit
    Left = 84
    Top = 60
    Width = 189
    Height = 21
    TabOrder = 1
    OnEnter = GlobalEnter
  end
  object EditPassword: TEdit
    Left = 84
    Top = 100
    Width = 189
    Height = 21
    EchoMode = emPassword
    TabOrder = 2
    OnEnter = GlobalEnter
  end
  object StatusBar1: TStatusBar
    Left = 0
    Top = 170
    Width = 308
    Height = 19
    Panels = <>
    SimplePanel = True
  end
end