Solved

Open / Save problem

Posted on 1998-03-26
3
141 Views
Last Modified: 2010-04-06
Hi,
    I'm trying to write code to ask the user if he wants to save the current document before opening another, if the document has changed. The problem is that the yes / no buttons don't do what I want them to! When pressed, they both just shut the dialog box like a cancel button would.

Here's the code:

unit Suredg;

interface

uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls, Buttons,
  StdCtrls, Main, ExtCtrls;

type
  Tsuredlg = class(TForm)
    Bevel1: TBevel;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  suredlg: Tsuredlg;

implementation

{$R *.DFM}

procedure Tsuredlg.BitBtn1Click(Sender: TObject);
begin
     MainForm.Saveas1Click(suredlg);
end;

procedure Tsuredlg.BitBtn2Click(Sender: TObject);
begin
     MainForm.EditPad.Modified := FALSE;
     MainForm.Open1Click(suredlg);

end;

end.


The MainForm has the following 2 procedures :

procedure TMainForm.Saveas1Click(Sender: TObject);
begin
      if SaveDialog1.Execute then
  begin
    EditPad.Lines.SaveToFile(SaveDialog1.FileName);
    OpenDialog1.Filename := SaveDialog1.FileName;
    Caption := SaveDialog1.FileName;
  end;

end;

procedure TMainForm.Open1Click(Sender: TObject);
begin
  if EditPad.Modified = TRUE then
     begin
     suredlg.ShowModal;
     exit;
     end;
  if OpenDialog1.Execute then
  begin
    EditPad.Lines.LoadfromFile(OpenDialog1.FileName);
    SaveDialog1.Filename := OpenDialog1.FileName;
    Caption := OpenDialog1.FileName;
  end;
end;


Does anyone know why it doesn't work?
Cheers

0
Comment
Question by:PiersBull
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
inter earned 20 total points
ID: 1360876
On

procedure TMainForm.Open1Click(Sender: TObject);
begin
  if EditPad.Modified = TRUE then
  begin
    suredlg.ShowModal; //What happends if user presses BitBtn2 RECURSIVE CALL!!!!
    exit; //here the function terminates regardless of the the value of ShowModal
  end;

In my opinion you should empty the BitBtn1 and BitBtn2 Click codes. Set their modal result to mrYes, mrNo respectively and do the following:

procedure TMainForm.Open1Click(Sender: TObject);
begin
  if EditPad.Modified then
  begin
    Suredlg.ShowModal;
    if Suredlg.ModalResult = idYes then
      Saveas1Click(Self);
  end;

  if OpenDialog1.Execute then
  begin
    EditPad.Lines.LoadfromFile(OpenDialog1.FileName);
    SaveDialog1.Filename := OpenDialog1.FileName;
    Caption := OpenDialog1.FileName;
    EditPad.Modified := false;
  end;
end;

Regards,
Igor
0
 

Author Comment

by:PiersBull
ID: 1360877
Cheers, Igor
   That seems to have done the trick! One problem, though. When I click 'yes' to save before opening the next document, then press 'cancel', it then opens the OpenDialog box, instead of quitting. I've tried sticking in:

procedure TMainForm.Open1Click(Sender: TObject);
begin
  if EditPad.Modified then
  begin
    Suredlg.ShowModal;
    if Suredlg.ModalResult = idYes then
      Saveas1Click(Self);
    if Suredlg.ModalResult = idCancel then
      exit;
 end;
.
.
.
.

This doesn't seem to do anything. I've set the ModalResult value of the cancel button to mrcancel so that's not the problem.  Is it just exiting out the if .. statement and not the procedure? If so, how can I get it to jump right out?

Cheers.

0
 
LVL 5

Expert Comment

by:inter
ID: 1360878
Hi friend,

if Exit code be executed the procedure sure be terminated. That seems completely correct syntaticaly, but semantically there is a problem with values mrYes, idYes or mrCancel, idCancel values. And the code above should work!

Bye,
Igor
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question