Solved

Open / Save problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 100
Delphi: how to implement a User Shortcut mapper? 1 95
Multiple image collision 13 69
Delphi inherited method 6 63
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

26 Experts available now in Live!

Get 1:1 Help Now