Logo New book: Delphi 2007 Handbook
My blog in online
Delphi tech support service: support.marcocantu.com
Google
  Web www.marcocantu.com

Menu for Development

Site Menu
Delphi 2007 Handbook
Mastering Borland Delphi 2005
Essential Pascal
Essential Delphi
Buy Books Online
Code Repository
Newsgroups
White Papers
Tools
Conferences
Training
Delphi Links
Contact Marco

My Other Sites
Italian Site (www.marcocantu.it)
Developers Newsgroups Browser (dev.newswhat.com)
My town (www.piazzacavalli.net)
the delphi search
Wintech Italia (my company)

Breaking News
Buy Mastering Borland Delphi 2005 from Amazon
Free ebook: Mastering Delphi Update for Delphi 2006

Advertising
Home My Blog Books My Bookstore Development Links Marco


Home: Code Repository: Mastering Delphi 6

Chapter 04 - Project StrDemo

Project Structure

StrDemo.dpr
program StrDemo;

uses
  Forms,
  StrForm in 'StrForm.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
StrForm.pas
unit StrForm;

interface

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

type
  TForm1 = class(TForm)
    EditResemble1: TEdit;
    EditResemble2: TEdit;
    ButtonResemble: TButton;
    ListBoxMatch: TListBox;
    EditMatch: TEdit;
    ButtonMatches: TButton;
    ButtonIndex: TButton;
    EditSample: TEdit;
    ButtonTriplicate: TButton;
    ButtonReverse: TButton;
    ButtonRandom: TButton;
    procedure ButtonResembleClick(Sender: TObject);
    procedure ButtonMatchesClick(Sender: TObject);
    procedure ButtonIndexClick(Sender: TObject);
    procedure ButtonTriplicateClick(Sender: TObject);
    procedure ButtonReverseClick(Sender: TObject);
    procedure ButtonRandomClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    strArray: array of string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses
  StrUtils;

procedure TForm1.ButtonResembleClick(Sender: TObject);
begin
  ShowMessage (BoolToStr (AnsiResemblesText (EditResemble1.Text, EditResemble2.Text), True));
end;

procedure TForm1.ButtonMatchesClick(Sender: TObject);
begin
  ShowMessage (BoolToStr (AnsiMatchText(EditMatch.Text, strArray), True));
end;

procedure TForm1.ButtonIndexClick(Sender: TObject);
var
  nMatch: Integer;
begin
  nMatch := AnsiIndexText(EditMatch.Text, strArray);
  ShowMessage (IfThen (nMatch >= 0,
    'Matches the string number ' + IntToStr (nMatch),
    'No match'));
end;

procedure TForm1.ButtonTriplicateClick(Sender: TObject);
begin
  ShowMessage (DupeString (EditSample.Text, 3));
end;

procedure TForm1.ButtonReverseClick(Sender: TObject);
begin
  ShowMessage (ReverseString (EditSample.Text));
end;

procedure TForm1.ButtonRandomClick(Sender: TObject);
begin
  ShowMessage (RandomFrom (strArray));
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  SetLength (strArray, ListBoxMatch.Items.Count);
  for I := 0 to ListBoxMatch.Items.Count - 1 do
    strArray [I] := ListBoxMatch.Items [I];

  // set the random seed, for the RandomFrom calls
  Randomize;
end;

end.
StrForm.dfm
object Form1: TForm1
  Left = 196
  Top = 112
  Width = 489
  Height = 334
  Caption = 'StrDemo'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object EditResemble1: TEdit
    Left = 64
    Top = 24
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Cantù'
     end
  object EditResemble2: TEdit
    Left = 296
    Top = 24
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'Cantu'
  end
  object ButtonResemble: TButton
    Left = 200
    Top = 24
    Width = 75
    Height = 25
    Caption = 'Resembles?'
    TabOrder = 2
    OnClick = ButtonResembleClick
  end
  object ListBoxMatch: TListBox
    Left = 296
    Top = 88
    Width = 121
    Height = 113
    ItemHeight = 13
    Items.Strings = (
      'one'
      'two'
      'three'
      'four'
      'five'
      'six'
      'seven'
      'eight')
    TabOrder = 3
  end
  object EditMatch: TEdit
    Left = 64
    Top = 128
    Width = 121
    Height = 21
    TabOrder = 4
    Text = 'four'
  end
  object ButtonMatches: TButton
    Left = 200
    Top = 96
    Width = 75
    Height = 25
    Caption = 'Matches Text'
    TabOrder = 5
    OnClick = ButtonMatchesClick
  end
  object ButtonIndex: TButton
    Left = 200
    Top = 128
    Width = 75
    Height = 25
    Caption = 'Index Text'
    TabOrder = 6
    OnClick = ButtonIndexClick
  end
  object EditSample: TEdit
    Left = 64
    Top = 232
    Width = 121
    Height = 21
    TabOrder = 7
    Text = 'sample text'
  end
  object ButtonTriplicate: TButton
    Left = 200
    Top = 216
    Width = 75
    Height = 25
    Caption = 'Triplicate'
    TabOrder = 8
    OnClick = ButtonTriplicateClick
  end
  object ButtonReverse: TButton
    Left = 200
    Top = 248
    Width = 75
    Height = 25
    Caption = 'Reverse'
    TabOrder = 9
    OnClick = ButtonReverseClick
  end
  object ButtonRandom: TButton
    Left = 200
    Top = 160
    Width = 75
    Height = 25
    Caption = 'Random'
    TabOrder = 10
    OnClick = ButtonRandomClick
  end
end