Solved

Open / Save problem

Posted on 1998-03-26
3
144 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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