home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / FINDREPL / MAIN.PAS < prev   
Pascal/Delphi Source File  |  1998-04-13  |  3KB  |  112 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.     ListBox1: TListBox;
  12.     FindDialog: TFindDialog;
  13.     FindBitBtn: TBitBtn;
  14.     CloseBitBtn: TBitBtn;
  15.     ReplaceBitBtn: TBitBtn;
  16.     ReplaceDialog: TReplaceDialog;
  17.     procedure FindBitBtnClick(Sender: TObject);
  18.     procedure FindDialogFind(Sender: TObject);
  19.     procedure ReplaceBitBtnClick(Sender: TObject);
  20.     procedure ReplaceDialogFind(Sender: TObject);
  21.     procedure ReplaceDialogReplace(Sender: TObject);
  22.   private
  23.     FindIndex, FoundPos, FoundLen: Integer;
  24.     FoundItem: Boolean;
  25.   public
  26.   end;
  27.  
  28. var
  29.   MainForm: TMainForm;
  30.  
  31. implementation
  32.  
  33. {$R *.DFM}
  34.  
  35. {- Begin a FindDialog operation }
  36. procedure TMainForm.FindBitBtnClick(Sender: TObject);
  37. begin
  38.   FindDialog.Execute;
  39.   FindIndex := 0;
  40.   ListBox1.ItemIndex := -1;
  41. end;
  42.  
  43. {- Continue a FindDialog operation }
  44. procedure TMainForm.FindDialogFind(Sender: TObject);
  45. var
  46.   S: String;
  47. begin
  48.   while FindIndex < ListBox1.Items.Count do
  49.   begin
  50.     S := ListBox1.Items[FindIndex];
  51.     Inc(FindIndex);
  52.     if Pos(FindDialog.FindText, S) <> 0 then
  53.     begin
  54.       ListBox1.ItemIndex := FindIndex - 1;
  55.       Exit;
  56.     end;
  57.   end;
  58.   ShowMessage('No more matches!');
  59.   FindDialog.CloseDialog;
  60. end;
  61.  
  62. {- Start a ReplaceDialog operation }
  63. procedure TMainForm.ReplaceBitBtnClick(Sender: TObject);
  64. begin
  65.   ReplaceDialog.Execute;
  66.   FindIndex := 0;
  67.   ListBox1.ItemIndex := -1;
  68.   FoundItem := False;
  69. end;
  70.  
  71. {- Continue a ReplaceDialog operation }
  72. procedure TMainForm.ReplaceDialogFind(Sender: TObject);
  73. var
  74.   S: String;
  75. begin
  76.   while FindIndex < ListBox1.Items.Count do
  77.   begin
  78.     S := ListBox1.Items[FindIndex];
  79.     Inc(FindIndex);
  80.     FoundPos := Pos(ReplaceDialog.FindText, S);
  81.     if FoundPos <> 0 then
  82.     begin
  83.       ListBox1.ItemIndex := FindIndex - 1;
  84.       FoundLen := Length(ReplaceDialog.FindText);
  85.       FoundItem := True;
  86.       Exit;
  87.     end;
  88.   end;
  89.   ShowMessage('No more matches!');
  90.   ReplaceDialog.CloseDialog;
  91. end;
  92.  
  93. {- Perform replacement for a ReplaceDialog operation }
  94. procedure TMainForm.ReplaceDialogReplace(Sender: TObject);
  95. var
  96.   S: String;
  97. begin
  98.   if frReplaceAll in ReplaceDialog.Options then
  99.     ShowMessage('Replace All not implemented')
  100.   else if not FoundItem then
  101.     ShowMessage('Click Find to begin/continue search')
  102.   else begin
  103.     S := ListBox1.Items[FindIndex - 1];
  104.     Delete(S, FoundPos, FoundLen);
  105.     Insert(ReplaceDialog.ReplaceText, S, FoundPos);
  106.     ListBox1.Items[FindIndex - 1] := S;
  107.     FoundItem := False;
  108.   end;
  109. end;
  110.  
  111. end.
  112.