home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 24 / PCPLUS115.iso / pcplus / delphi / editor2 / edit2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-02  |  3.0 KB  |  119 lines

  1. unit Edit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Menus, StdCtrls,
  8.   IOProcs; 
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     MainMenu1: TMainMenu;
  13.     Memo1: TMemo;
  14.     OpenDialog1: TOpenDialog;
  15.     SaveDialog1: TSaveDialog;
  16.     FileMnu: TMenuItem;
  17.     FileNew: TMenuItem;
  18.     FileSave: TMenuItem;
  19.     FileLoad: TMenuItem;
  20.     N1: TMenuItem;
  21.     FileExit: TMenuItem;
  22.     FileSaveAs: TMenuItem;
  23.     procedure FileNewClick(Sender: TObject);
  24.     procedure FileSaveClick(Sender: TObject);
  25.     procedure FileSaveAsClick(Sender: TObject);
  26.     procedure FileLoadClick(Sender: TObject);
  27.     procedure FileExitClick(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.   public
  31.     { Public declarations }
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.DFM}
  40.  
  41.  
  42. function ConfirmFileSave(FileName : string) : boolean;
  43. { put up a dialg box to confirm that the existing file should be
  44.   overwritten. This function returns True if the Yes button is pressed,
  45.   otherwise it returns false. Note that you can test for the
  46.   buttons used in dialogs with these constants: mrNone,mrOk,mrCancel,
  47.   mrAbort,mrRetry,mrIgnore,mrYes,mrNo,mrAbort,mrRetry,mrIgnore,mrAll }
  48.  
  49. begin
  50.     if MessageDlg(FileName + ' already exists. Save anyway?',
  51.                         mtConfirmation, mbYesNoCancel, 0)
  52.                         = mrYes then
  53.       ConfirmFileSave := true
  54.     else
  55.       ConfirmFileSave := false;
  56. end;
  57.  
  58. procedure TForm1.FileNewClick(Sender: TObject);
  59. begin
  60.   Memo1.Clear;
  61.   OpenDialog1.Filename := '*.*';
  62.   Caption := 'Text Editor 2 - [Untitled]';
  63. end;
  64.  
  65. procedure TForm1.FileSaveClick(Sender: TObject);
  66. begin
  67.   if (OpenDialog1.Filename <> '')
  68.      and (OpenDialog1.Filename <> '*.*') then
  69.   begin
  70.     Memo1.Lines.SaveToFile(OpenDialog1.Filename);
  71.   end
  72.   else FileSaveAsClick(Sender);
  73. end;
  74.  
  75. procedure TForm1.FileSaveAsClick(Sender: TObject);
  76. var
  77.    SaveFile : boolean;
  78. begin
  79.    SaveFile := true;
  80.    with SaveDialog1 do
  81.     if Execute then
  82.     begin
  83.       if ThisFileExists(FileName) then
  84.          SaveFile := ConfirmFileSave(FileName);
  85.       If SaveFile then
  86.       begin
  87.          Memo1.Lines.SaveToFile(Filename);
  88.          Caption := 'Text Editor 2 - ' + ExtractFilename(FileName);
  89.          OpenDialog1.Filename := Filename;
  90.       end;
  91.     end;
  92. end;
  93.  
  94. procedure TForm1.FileLoadClick(Sender: TObject);
  95. begin
  96.   with OpenDialog1 do
  97.     if Execute then
  98.     begin
  99.       { if the specified file exists it loads it, otherwise
  100.         it displays an error message }
  101.       if ThisFileExists(FileName) Then
  102.       begin
  103.           Memo1.Lines.LoadFromFile(FileName);
  104.           Caption := 'Text Editor 2 - ' + ExtractFilename(FileName);
  105.       end
  106.       else
  107.         MessageDlg('Sorry. Can''t load this file. '+ FileName +
  108.                            ' does not exist!',
  109.                         mtInformation, [mbOK], 0);
  110.     end;
  111. end;
  112.  
  113. procedure TForm1.FileExitClick(Sender: TObject);
  114. begin
  115.   Close;
  116. end;
  117.  
  118. end.
  119.