Solved

Close Dialog Box

Posted on 1998-03-31
12
220 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

827 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