Solved

Close Dialog Box

Posted on 1998-03-31
12
222 Views
Last Modified: 2010-04-06
Hi,
   In a close dialog box, how do you get the modal result? For example, I want to get a result if the user hits cancel, so that I can exit the current procedure.
Cheers.
0
Comment
Question by:PiersBull
  • 6
  • 5
12 Comments
 
LVL 4

Expert Comment

by:d003303
ID: 1361120
Hi again,
the easiest way is to use the Application.MessageBox function. It takes 2 PChar strings and on flag that defines the buttons, dialog box state and icon. The function returns id values that identify the button that is pressed. An example for a Yes/No/Cancel dialog with an icon :

DlgResult := Application.MessageBox('File not saved. Save it now ?', 'Note', MB_YESNOCANCEL or MB_ICONEXCLAMATION);
if DlgResult = IDYES then ...
if DlgResult = IDNO then ...
if DlgResult = IDCANCEL then ...

Cheers,
Slash/d003303
0
 

Author Comment

by:PiersBull
ID: 1361121
Hi again,
   That's not actually the problem I've got. Try this.
I've got a dialog box which asks eg. if the user wants to save current file before opening the next. If the user hits cancel, it cancels. If the user hits no, open dialog appears. However, if the user hits yes to save, then hits cancel on the save dialog box, it goes ahead and opens the open dialog box. I want it to cancel completely if the user hits cancel on the save dialog box.
Any help?
 
0
 
LVL 4

Expert Comment

by:d003303
ID: 1361122
Yeah, I see. Use

if SaveDialog1.Execute = false
 then CancelEverthingCompletely
 else GoAheadSaveTheFileAndOpenTheNextOne;

assuming the CancelEverthingCompletely and GoAheadSaveTheFileAndOpenTheNextOne procedures exist ;-))

CU,
Slash/d003303
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PiersBull
ID: 1361123
Sorry I'm being thick, but what would you write in the CancelEverythingCompletely procedure? My save procedure looks like this :

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


  FileName := Caption;
end;

Cheers, Nik
(Points are up a bit for your perseverance!)

0
 

Expert Comment

by:satto
ID: 1361124
Okey

Great 2 form (form1) and (form2)
In (form1) that put 1 button (Button1)
And in (form2) put 2 button, (Button1 and Button2).

Mark the (Button1) in form 2, and goto "Object Inspector/Properties/ModalResult" sitt that to "mrYes", and for (Button2) sitt that to "mrNo".

In (form1) DDclick on (Button1) and put this form in to that.

procedure TForm1.Button1Click(Sender: TObject);
begin
  form2.showmodal;
  if form2.ModalResult = mrYes then
    Beep
  else
    Application.Terminate;
end;

Now u are running.

Satto
0
 
LVL 4

Expert Comment

by:d003303
ID: 1361125
OK,
I think you should re-design your program a bit. Take a look at the code. I assumed that there is a FileName property in EditPad that is empty for an untitled file and a NewFile method that creates a new empty document.


procedure TMainForm.Save(FileName: string);
begin
  EditPad.Lines.SaveToFile(FileName);
  Caption := ExtractFileName(FileName);
  EditPad.Modified := FALSE;
  EditPad.FileName := FileName;
end;

function TMainForm.SaveAs: Boolean;
begin
  Result := SaveDialog1.Execute;
  if Result
   then Save(SaveDialog1.FileName);
end;

procedure TMainForm.Open(FileName: string);
begin
  EditPad.Lines.LoadFromFile(FileName);
  Caption := ExtractFileName(FileName);
  EditPad.Modified := FALSE;
  EditPad.FileName := FileName;
end;

function TMainForm.OpenAs: Boolean;
begin
  Result := OpenDialog1.Execute;
  if Result
   then Open(OpenDialog1.FileName);
end;

function TMainForm.SaveIt: Boolean;
begin
  Result := true;
  if EditPad.FileName <> ''
   then Save(EditPad.FileName)
   else if not SaveAs
         then Result := false;
end;

function TMainForm.CheckIfDirty: Boolean;
var DlgResult: LongInt;
begin
  Result := false
  if EditPad.Modified then
   begin
     DlgResult := Application.MessageBox('Text modified. Save it now ?', 'Note', MB_YESNOCANCEL or MB_ICONEXCLAMATION);
     if DlgResult = IDCancel
      then Exit;
     if DlgResult = IDYes then
      if not SaveIt
       then Exit;
     Result := true
   end;
end;

procedure TMainForm.SaveAsButtonClick(Sender: TObject);
var Buffer : string;
begin
  Buffer := EditPad.FileName;
  if not SaveIt
   then EditPad.FileName = Buffer;
end;

procedure TMainForm.SaveButtonClick(Sender: TObject);
begin
  SaveIt;
end;

procedure TMainForm.NewButtonClick(Sender: TObject);
begin
  if CheckIfDirty
   then NewFile;
end;

procedure TMainForm.OpenButtonClick(Sender: TObject);
begin
  if CheckIfDirty
   then OpenAs;
end;

Slash/d003303
0
 
LVL 4

Expert Comment

by:d003303
ID: 1361126
oops, one mistake in this method :

procedure TMainForm.SaveAsButtonClick(Sender: TObject);
var Buffer : string;
begin
  Buffer := EditPad.FileName;
  // here comes the missing line
  EditPad.FileName := '';
  if not SaveIt
   then EditPad.FileName = Buffer;
end;

Slash/d003303
0
 

Author Comment

by:PiersBull
ID: 1361127
Hi again Slash.

I seem to be having difficulty getting my head round this. I've implemented the code you offered and it doesn't seem to do what it should. I don't think CheckIfDirty is working properly. I've doubled the points by the way as you've put so much time into it.

I've got a global variable FileN : string  in MainForm.
The code I now have is as follows :

procedure TMainForm.Save(FileName: string);
begin
     EditPad.Lines.SaveToFile(FileName);
     Caption := ExtractFileName(FileName);
     EditPad.Modified := FALSE;
     FileN := Caption;
end;

function TMainForm.SaveAs: Boolean;
begin
     Result := SaveDialog1.Execute;
     if Result
     then Save(SaveDialog1.FileName);
end;

procedure TMainForm.Open(FileName: string);
begin
      EditPad.Lines.LoadFromFile(FileName);
      Caption := ExtractFileName(FileName);
      EditPad.Modified := FALSE;
      FileN := Caption;
end;

function TMainForm.OpenAs: Boolean;
begin
      Result := OpenDialog1.Execute;
      if Result
      then Open(OpenDialog1.FileName);
end;

function TMainForm.SaveIt: Boolean;
begin
     Result := true;
     if FileN <> 'new.tex'
     then Save(FileN)
     else if not SaveAs
     then Result := false;
end;

function TMainForm.CheckIfDirty: Boolean;
var
   DlgResult: LongInt;
begin
     Result := false;
     if EditPad.Modified then
     begin
          DlgResult := Application.MessageBox('Text modified. Save it now ?', 'Note', MB_YESNOCANCEL or MB_ICONEXCLAMATION);
          if DlgResult = IDCancel
          then Exit;
          if DlgResult = IDYes then
          if not SaveIt
          then Exit;
          Result := true;
     end;
end;


procedure TMainForm.Open1Click(Sender: TObject);
begin
     if not CheckIfDirty
     then OpenAs;
end;

procedure TMainForm.Save1Click(Sender: TObject);
begin
  SaveIt;
end;

procedure TMainForm.Saveas1Click(Sender: TObject);
var Buffer : string;
begin
     Buffer := FileN;
     FileN := 'new.tex';
     if not SaveIt
     then FileN := Buffer;
end;

procedure TMainForm.NewClick(Sender: TObject);
begin
     if not CheckIfDirty then
        NewFile(New);
end;

Most of it works OK. By the way, a new file is called 'new.tex', not simply left blank. If the editpad is not modified, all the code works. If it is modified, the messagebox appears but it seems to exit whatever button you press. I have no idea why.

I appreciate your help on this one. Cheers, Nik.

PS. How can you have a variable 'Result' which isn't declared anywhere?

0
 
LVL 4

Accepted Solution

by:
d003303 earned 40 total points
ID: 1361128
OK, got the errors :

procedure TMainForm.Open1Click(Sender: TObject);
begin
  // removed 'not'
  if CheckIfDirty
   then OpenAs;
end;

procedure TMainForm.NewClick(Sender: TObject);
begin
  // removed 'not'
  if CheckIfDirty then
   NewFile(New);
end;

Now it should work.
In a function, the Result variable is automatically declared as the functions result type and carries the functions result value.

Slash/d003303
0
 

Author Comment

by:PiersBull
ID: 1361129
Hi again,
   I've changed the code as you suggest , but that gives me problems. Now, when I go to file - open, it doesn't do anything at all!
Cheers, Nik

0
 
LVL 4

Expert Comment

by:d003303
ID: 1361130
OK, hopefuly the final fix :

function TMainForm.CheckIfDirty: Boolean;
var
   DlgResult: LongInt;
begin
     Result := false;
     if EditPad.Modified then
     begin
          DlgResult := Application.MessageBox('Text modified. Save it now ?', 'Note', MB_YESNOCANCEL or MB_ICONEXCLAMATION);
          if DlgResult = IDCancel
          then Exit;
          if DlgResult = IDYes then
          if not SaveIt
          then Exit;
          Result := true;
// added the else clause
     end
     else Result := true;
end;

Sorry, I did not double-check the code ;-)

Slash/d003303
0
 

Author Comment

by:PiersBull
ID: 1361131
Excellent! Cheers for all your help...

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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