Solved

Open / Save problem

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now