Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 6

Chapter 06 - Project InFocus

Project Structure

InFocus.dpr
program InFocus;

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

{$R *.RES}

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

interface

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

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 *.DFM}

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.dfm
object FocusForm: TFocusForm
  Left = 255
  Top = 110
  Width = 308
  Height = 188
  ActiveControl = EditFirstName
  Caption = 'InFocus'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clBlack
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 16
    Top = 24
    Width = 48
    Height = 13
    HelpType = htKeyword
    Caption = '&First name'
    FocusControl = EditFirstName
  end
  object Label2: TLabel
    Left = 16
    Top = 64
    Width = 49
    Height = 13
    HelpType = htKeyword
    Caption = '&Last name'
    FocusControl = EditLastName
  end
  object Label3: TLabel
    Left = 16
    Top = 104
    Width = 46
    Height = 13
    HelpType = htKeyword
    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
    PasswordChar = '*'
    TabOrder = 2
    OnEnter = GlobalEnter
  end
  object StatusBar1: TStatusBar
    Left = 0
    Top = 142
    Width = 300
    Height = 19
    Panels = <>
    SimplePanel = True
  end
end