Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Open / Save problem

Posted on 1998-03-26
3
143 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 115
Find and Replace Stream with 0s 8 69
Base1 Encode/Decode 3 81
Is there any TOOL for creating database *.CDS[ClientDataSet] ,*.FDS,*.FDB,*.ADB? 6 45
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…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

839 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