• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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
0
peterkiers
Asked:
peterkiers
  • 3
  • 2
1 Solution
 
MerijnBSr. Software EngineerCommented:
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.
0
 
peterkiersAuthor Commented:
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
0
 
MerijnBSr. Software EngineerCommented:
I think you have the questions you want to ask the user and the actions you want to take to those answers not correct.
Can you try to explain what you want in words, not in code, because I think the pieces like:

                                    Save1Click(Sender); //do a save or save as
                                     LoadOptions(CfgFile); //load the default file

and

                                   mrNo: LoadOptions(CfgFile); //load the default file
                                    mrCancel: ;//do nothing

will not do what you think they should do.
0
 
peterkiersAuthor Commented:
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
0
 
peterkiersAuthor Commented:
I give up
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now