home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / STRLIST / MAIN.PAS < prev    next >
Pascal/Delphi Source File  |  1998-04-13  |  4KB  |  152 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     AddButton: TButton;
  12.     CloseBitBtn: TBitBtn;
  13.     GetButton: TButton;
  14.     ListBox1: TListBox;
  15.     UpdateButton: TButton;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure FormDestroy(Sender: TObject);
  18.     procedure AddButtonClick(Sender: TObject);
  19.     procedure UpdateButtonClick(Sender: TObject);
  20.     procedure GetButtonClick(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. var
  28.   MainForm: TMainForm;
  29.  
  30. implementation
  31.  
  32. {$R *.DFM}
  33.  
  34. type
  35.  
  36. {- New class of items to insert in a list }
  37.   TStrItem = class
  38.     S: string;
  39.     I: Integer;
  40.     constructor Create(S1: string; I1: Integer);
  41.   end;
  42.  
  43. {- Derived class to hold TStrItem objects }
  44.   TStrList = class(TList)
  45.     destructor Destroy; override;
  46.     function FindItem(S1: string): TStrItem;
  47.   end;
  48.  
  49. var
  50.   StrList: TStrList;      { List of TStrItems }
  51.   ItemCount: Integer;     { Number of items inserted }
  52.  
  53. {- Create a new instance of TStrItem }
  54. constructor TStrItem.Create(S1: String; I1: Integer);
  55. begin
  56.   S := S1;                { Save string parameter }
  57.   I := I1;                { Save integer parameter }
  58.   inherited Create;       { Call inherited Create }
  59. end;
  60.  
  61. {- Destroy instance of TStrList }
  62. destructor TStrList.Destroy;
  63. var
  64.   I: Integer;
  65. begin
  66.   for I := 0 to Count - 1 do
  67.     TStrItem(Items[I]).Free;  { Free all TStrItems }
  68.   inherited Destroy;          { Call inherited destroy }
  69. end;
  70.  
  71. {- Return object identified by S1 or nil for no match }
  72. function TStrList.FindItem(S1: string): TStrItem;
  73. var
  74.   I: Integer;
  75.   P: TStrItem;
  76. begin
  77.   for I := 0 to Count - 1 do
  78.   begin
  79.     P := TStrItem(Items[I]);  { P refers to a TStrItem object }
  80.     if Uppercase(P.S) = Uppercase(S1) then  { Match? }
  81.     begin           { Found match }
  82.       Result := P;  { Return P as function result }
  83.       Exit;         { Exit function immediately }
  84.     end;
  85.   end;
  86.   Result := nil;    { No match; return nil }
  87. end;
  88.  
  89. {- TMainForm event handlers }
  90.  
  91. procedure TMainForm.FormCreate(Sender: TObject);
  92. begin
  93.   StrList := TStrList.Create;  { Create new StrList object }
  94.   ItemCount := 0;              { Initialize insertion count }
  95. end;
  96.  
  97. procedure TMainForm.FormDestroy(Sender: TObject);
  98. begin
  99.   StrList.Free;  { Also destroys listed items }
  100. end;
  101.  
  102. {- Button event handlers }
  103.  
  104. procedure TMainForm.AddButtonClick(Sender: TObject);
  105. var
  106.   StrItem: TStrItem;   { New item to insert }
  107.   S: string;           { User input string }
  108. begin
  109.   S := '';
  110.   if InputQuery(Caption, 'Enter item', S) then
  111.   if Length(S) > 0 then
  112.   begin
  113.     Inc(ItemCount);
  114.     StrItem := TStrItem.Create(S, ItemCount);
  115.     StrList.Add(StrItem);
  116.   end;
  117. end;
  118.  
  119. procedure TMainForm.UpdateButtonClick(Sender: TObject);
  120. var
  121.   I: Integer;
  122.   P: TStrItem;
  123. begin
  124.   ListBox1.Clear;
  125.   with StrList do
  126.   for I := 0 to Count - 1 do
  127.   begin
  128.     P := TStrItem(StrList.Items[I]);
  129.     ListBox1.Items.Add(Format('#%d: %s', [P.I, P.S]));
  130.   end;
  131. end;
  132.  
  133. procedure TMainForm.GetButtonClick(Sender: TObject);
  134. var
  135.   S: string;
  136.   P: TStrItem;
  137. begin
  138.   UpdateButton.Click;
  139.   S := '';
  140.   if InputQuery(Caption, 'Enter item name', S) then
  141.   if Length(S) > 0 then
  142.   begin
  143.     P := StrList.FindItem(S);
  144.     if P <> nil then
  145.       ShowMessage(Format('%s, Number = %d', [P.S, P.I]))
  146.     else
  147.       ShowMessage('No such item');
  148.   end;
  149. end;
  150.  
  151. end.
  152.