Mega Code Archive

 
Categories / Delphi / Examples
 

Opensavegood

USEFUL CODE FOR OPENING AND SAVING STUFF INCLUDING CHECKS TO SEE IF YOUR MEMO HAS BEEN MODIFIED procedure TIdiomForm.CheckFileSave; var SaveResp: Integer; begin if not IdiomMemo.Modified then Exit; SaveResp := MessageDlg(Format('Save changes to %s?', [currFileName]), mtConfirmation, mbYesNoCancel, 0); case SaveResp of idYes: FileSave(Self); {do nothing if the response is idNo...} idNo: ; idCancel: Abort; end; end; procedure TIdiomForm.FileSave(Sender: TObject); begin if currFileName = 'Untitled' then FileSaveAs(Sender) else begin IdiomMemo.Lines.SaveToFile(currFileName); IdiomMemo.Modified := False; end; end; procedure TIdiomForm.OpenButtonClick(Sender: TObject); begin CheckFileSave; if OpenDialog.Execute then begin IdiomMemo.Lines.LoadFromFile(OpenDialog.FileName); SetFileName(OpenDialog.FileName); IdiomMemo.SetFocus; IdiomMemo.Modified := False; IdiomMemo.ReadOnly := ofReadOnly in OpenDialog.Options; end; end; procedure TIdiomForm.SetFileName(const FileName: String); begin currFileName := FileName; Caption := Format('%s - %s', [ExtractFileName(FileName), Application.Title]); end; procedure TIdiomForm.SaveButtonClick(Sender: TObject); begin if currFileName = 'Untitled' then FileSaveAs(Sender) else begin ClueMemo.Lines.SaveToFile(currFileName); ClueMemo.Modified := False; end; end; procedure TIdiomForm.FileSaveAs(Sender: TObject); begin if SaveDialog.Execute then begin if FileExists(SaveDialog.FileName) then if MessageDlg(Format('OK to overwrite %s', [SaveDialog.FileName]), mtConfirmation, mbYesNoCancel, 0) <> idYes then Exit; ClueMemo.Lines.SaveToFile(SaveDialog.FileName); SetFileName(SaveDialog.FileName); ClueMemo.Modified := False; end; end;