home *** CD-ROM | disk | FTP | other *** search
- { This demo shows how to use the TOpenDialog,
- TSaveDialog, TFindDialog and TReplaceDialog
- components along with the LoadFromFile and
- SaveToFile methods of a TStringList (in this
- case the string list in Memo1). }
-
- unit Main;
-
- interface
-
- uses WinTypes, WinProcs, Classes, Graphics, Controls, Menus,
- Forms, StdCtrls, Dialogs, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- Memo1: TMemo;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- FindDialog1: TFindDialog;
- Search1: TMenuItem;
- Find1: TMenuItem;
- Replace1: TMenuItem;
- FindNext1: TMenuItem;
- ReplaceDialog1: TReplaceDialog;
- procedure New1Click(Sender: TObject);
- procedure FileOpenClick(Sender: TObject);
- procedure Save1Click(Sender: TObject);
- procedure SaveAs1Click(Sender: TObject);
- procedure FileExitClick(Sender: TObject);
- procedure Find1Click(Sender: TObject);
- procedure Find(Sender : TObject);
- procedure FindNext1Click(Sender: TObject);
- procedure Replace1Click(Sender: TObject);
- procedure Replace(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses SysUtils, Search;
-
- {$R *.DFM}
-
- procedure TForm1.New1Click(Sender: TObject);
- begin
- Memo1.Clear;
- OpenDialog1.Filename := '';
- Caption := 'Text Demo - [Untitled]';
- end;
-
- procedure TForm1.FileOpenClick(Sender: TObject);
- begin
- with OpenDialog1 do
- if Execute then
- begin
- Memo1.Lines.LoadFromFile(FileName);
- Caption := 'Text Demo - ' + ExtractFilename(FileName);
- end;
- end;
-
- procedure TForm1.Save1Click(Sender: TObject);
- begin
- if OpenDialog1.Filename <> '' then
- begin
- Memo1.Lines.SaveToFile(OpenDialog1.Filename);
- end
- else SaveAs1Click(Sender);
- end;
-
- procedure TForm1.SaveAs1Click(Sender: TObject);
- begin
- with SaveDialog1 do
- if Execute then
- begin
- Memo1.Lines.SaveToFile(Filename);
- Caption := 'Text Demo - ' + ExtractFilename(FileName);
- OpenDialog1.Filename := Filename;
- end;
- end;
-
- procedure TForm1.Find1Click(Sender: TObject);
- begin
- FindDialog1.Execute;
- FindNext1.Enabled := True;
- end;
-
- procedure TForm1.Find(Sender: TObject);
- begin
- with Sender as TFindDialog do
- if not SearchMemo(Memo1, FindText, Options) then
- ShowMessage('Cannot find "' + FindText + '".');
- end;
-
- procedure TForm1.Replace1Click(Sender: TObject);
- begin
- ReplaceDialog1.Execute;
- end;
-
- procedure TForm1.FindNext1Click(Sender: TObject);
- begin
- Find(FindDialog1);
- end;
-
- { Replace and ReplaceAll call this routine. }
- procedure TForm1.Replace(Sender: TObject);
- var
- Found: Boolean;
- begin
- with ReplaceDialog1 do
- begin
- if AnsiCompareText(Memo1.SelText, FindText) = 0 then
- Memo1.SelText := ReplaceText;
- Found := SearchMemo(Memo1, FindText, Options);
- while Found and (frReplaceAll in Options) do
- begin
- Memo1.SelText := ReplaceText;
- Found := SearchMemo(Memo1, FindText, Options);
- end;
- if (not Found) and (frReplace in Options) then
- ShowMessage('Cannot find "' + FindText + '".');
- end;
- end;
-
- procedure TForm1.FileExitClick(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- var
- MsgResult: Word;
- begin
- if Memo1.Modified then
- MsgResult := MessageDlg(Format('File %s has been modified. Save file?',
- [OpenDialog1.Filename]), mtWarning, mbYesNoCancel, 0);
- case MsgResult of
- mrYes:
- begin
- Save1Click(Sender);
- CanClose := True;
- end;
- mrNo: CanClose := True;
- mrCancel: CanClose := False;
- end;
- end;
-
- end.
-