Close Dialog Box

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.
PiersBullAsked:
Who is Participating?
 
d003303Connect With a Mentor Commented:
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
 
d003303Commented:
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
 
PiersBullAuthor Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
d003303Commented:
Yeah, I see. Use

if SaveDialog1.Execute = false
 then CancelEverthingCompletely
 else GoAheadSaveTheFileAndOpenTheNextOne;

assuming the CancelEverthingCompletely and GoAheadSaveTheFileAndOpenTheNextOne procedures exist ;-))

CU,
Slash/d003303
0
 
PiersBullAuthor Commented:
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
 
sattoCommented:
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
 
d003303Commented:
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
 
d003303Commented:
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
 
PiersBullAuthor Commented:
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
 
PiersBullAuthor Commented:
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
 
d003303Commented:
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
 
PiersBullAuthor Commented:
Excellent! Cheers for all your help...

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.