Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Look if the file is modified.

Dear Experts,

In my previous question I have asked how to implement Save and SaveAs, I have
got a good solution, in that question Bobi100 gave me a good advice. This is what
he said:

"one more thing that would improve your application is an isModified flag, try adding a boolean property in your form, make sure this flag is set to False on create, after a successful save, after new, and after opening. Set it to True on any change done to whatever input you have in your form, then, wrap the case statement statement in the code above with this:"

  if IsModified then
    case ...................
  else
    LoadOptions(CfgFile); //load the default file automatically because the file was not modified

Only I don’t know how to implement his advice !
This code below is what I got sofar. Can someone help me?


(*-----------------------------------------------*)
procedure TMainForm.Open1Click(Sender: TObject);
begin
  if socOpen then Warning2
  else begin
    OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
    OpenDialog1.DefaultExt := 'emu';
    if OpenDialog1.Execute then
    begin
      LoadOptions(OpenDialog1.Filename);
      CurrentFile := OpenDialog1.FileName;
      MakeConnect;
    end;
  end;
end;
(*-----------------------------------------------*)
procedure TMainForm.New1Click(Sender: TObject);
begin
  if socOpen then Warning1
  else Connect1Click(sender);
end;
(*-----------------------------------------------*)
procedure TMainForm.Save1Click(Sender: TObject);
begin
  if (CurrentFile = CfgFile) then
    SaveAs1Click(sender)
  else
    SaveOptions(CurrentFile)
end;
(*-----------------------------------------------*)
procedure TMainForm.SaveAs1Click(Sender: TObject);
begin
  SaveDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
  SaveDialog1.DefaultExt := 'emu';
  if SaveDialog1.Execute then
  begin
    if SaveDialog1.Filename <> CfgFile then
    begin
      SaveOptions(SaveDialog1.Filename);
      CurrentFile := SaveDialog1.Filename;
    end
    else
      ShowMessage('Can''t write to default file');
  end;
end;
(*-----------------------------------------------*)


Greetings,

Peter Kiers
Avatar of MerijnB
MerijnB
Flag of Netherlands image

I don't really know what you are making here, but it seems like you'll have to use the IsModified flag in your SaveOptions() routine:

procedure TMainForm.SaveOptions(FileName: string);
begin
 if IsModified then   // only save if options are modified
 begin
  // do your saving stuff here
 end;
end;

This means, like Bobi100 said you'll have to set IsModified to false whenever you save, open or make a new file and to true.
This is surely a nice addition, but if saving the options doesn't take long you might want to just always save and don't use this extra boolean.
Avatar of Peter Kiers

ASKER

Maybe we should take this step bij step, because I don't follow anymore...
and I am trying and trying but no results.

The procedure New1Click should first look if the file that is loaded
hasn't been changed.

If so then show message:  'Do you want to save the current file?'

if user clicks Yes then        
                                    Save1Click(Sender); //do a save or save as
                                     LoadOptions(CfgFile); //load the default file
if user clicks No then
                                   mrNo: LoadOptions(CfgFile); //load the default file
                                    mrCancel: ;//do nothing
 
Then if the user clicks YES or NO on the message
The second message has to appear, to determine if there is a connection
if so show warning else Connect1Click:

 if socOpen then Warning1     //if socopen is true then showmessage
 else Connect1Click(sender);  //else show message connect1click


I am trying to write that, this is the totally wrong code:

procedure TMainForm.New1Click(Sender: TObject);
begin
  if socOpen then Warning1
  else Connect1Click(sender);

  begin
  case MessageDlg('Do you want to save the current file?', mtConfirmation,
                  [mbYes, mbNo, mbCancel],0) of
    mrYes:
    begin
      Save1Click(Sender); //do a save or save as
      LoadOptions(CfgFile); //load the default file
    end;
    mrNo: LoadOptions(CfgFile); //load the default file
    mrCancel: ;//do nothing
  end
end;
end;

Peter
ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oke, this isn't going wright, so I try another approach.

I have a little other problem, maybe you could help me with that
and forget the rest, and then the 500 points are all yours.

I want the file CfgFile to be loaded when the programm startsup
and when the user clicks on the toolbutton New.

I have allready the code to start the file at the startup of the programm:

  CfgFile := IncludeTrailingBackslash(ExeDir) + 'Emu3270.emr';
  CurrentFile := CfgFile;
  if LoadOptions(CfgFile) then
    ShowMessage('Warning: Errors found in config file: ' + CfgFile);

But the New-button doesn't reload the CfgFile.

This is what I got.

procedure TMainForm.New1Click(Sender: TObject);
begin
  if socOpen then Warning1                  //if there is a connection show message
  else Connect1Click(sender);               //else start form Connect.
end;

procedure TMainForm.Warning1;
begin
  if (Application.MessageBox(
    'You must disconnect before opening another connection.' + #10 + #13 +
    'Do you want to disconnect?',
    'Access2All',
    MB_YESNO + MB_DEFBUTTON1 + MB_ICONEXCLAMATION) = ID_YES) then
    begin
    Disconnect1Click(self);
    Connect1click(self);
    end;
end;

The code to save to file is : SaveOptions(CfgFile);
to code to load the file is : LoadOptions(CfgFile);

Peter





 



the programm
has to look if the file that's is loaded has been changed
if so
I give up