Recent

Author Topic: [SOLVED] Dialog open and save  (Read 366 times)

Pe3s

  • Full Member
  • ***
  • Posts: 242
[SOLVED] Dialog open and save
« on: July 26, 2022, 04:00:53 pm »
Hello, I found out the code that does not show the save dialog when the file is opened.
The code works, but I wonder if I can write it better.
Thank you for the hints
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   TmpPath: String;
  4.  
  5. implementation
  6.  
  7. {$R *.lfm}
  8.  
  9. { TForm1 }
  10.  
  11. procedure TForm1.SpeedButton1Click(Sender: TObject);
  12. begin
  13.   if OpenDialog1.Execute then
  14.   begin
  15.     Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
  16.     Edit1.Text:= OpenDialog1.FileName;
  17.  
  18.     TmpPath:= OpenDialog1.FileName;
  19.   end;
  20. end;
  21.  
  22. procedure TForm1.SpeedButton2Click(Sender: TObject);
  23. begin
  24.   case (TmpPath ='') of
  25.   True: begin
  26.     if SaveDialog1.Execute then
  27.     begin
  28.        Memo1.Lines.SaveToFile(SaveDialog1.FileName);
  29.        TmpPath:= SaveDialog1.FileName;
  30.     end;
  31.   end;
  32.   False: begin
  33.     Memo1.Lines.SaveToFile(TmpPath);
  34.   end;
  35. end;
  36. end;
  37.  
« Last Edit: July 26, 2022, 06:30:15 pm by Pe3s »

rvk

  • Hero Member
  • *****
  • Posts: 4935
Re: Dialog open and save
« Reply #1 on: July 26, 2022, 04:13:59 pm »
The use of "case boolean of" seems really weird.
You can just use the if statement there.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SpeedButton2Click(Sender: TObject);
  2. begin
  3.   if (TmpPath = '') then
  4.   begin
  5.     if SaveDialog1.Execute then
  6.     begin
  7.        Memo1.Lines.SaveToFile(SaveDialog1.FileName);
  8.        TmpPath:= SaveDialog1.FileName;
  9.     end;
  10.   end
  11.   else
  12.   begin
  13.       Memo1.Lines.SaveToFile(TmpPath);
  14.   end;
  15. end;

But because you always fill in TmpPath you can even do it more compact like this;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SpeedButton2Click(Sender: TObject);
  2. begin
  3.   if (TmpPath = '') then
  4.     if SaveDialog1.Execute then
  5.       TmpPath:= SaveDialog1.FileName;
  6.  
  7.   if TmpPath <> '' then
  8.     Memo1.Lines.SaveToFile(TmpPath);
  9. end;


Pe3s

  • Full Member
  • ***
  • Posts: 242
[SOLVED] Dialog open and save
« Reply #2 on: July 26, 2022, 06:29:53 pm »
Thank you  :)

 

TinyPortal © 2005-2018