Board index » delphi » Check the contents of a dialog before closing

Check the contents of a dialog before closing


2005-04-25 11:49:04 PM
delphi92
Hi, I have a dialog box that I diplay using ShowModal. I use it to allow the
user to modify different parameters. Now, when the user wants to validate
his/her changes (either by clicking on the OK button, selecting the OK
button and then pressing the space key, pressing the return key, etc.?),
then I want to check that all the changes actually make sense. If one or
several of them don't, I then want to let him/her know.
I have written the code to do all the tests and let the user know and it
works fine. The problem though is that I am not quite sure which event (if
any) to catch and call those tests... If I was to do that within the
OKBtn.OnClick event, I'd indeed go there no matter the way the user
tries to close the dialog box, but my problem is then how to prevent the
dialog box from actually closing if there are mistakes? To set
OKBtn.ModalResult to mrNone won't do unfortunately.
Surely, others have come across that problem before, no? I tried google, but
couldn't find anything relevant... then again, I may not have used the right
keywords?
Cheers, Alan.
 
 

Re:Check the contents of a dialog before closing

Quote
, but my problem is then how to prevent the
dialog box from actually closing if there are mistakes? To set
OKBtn.ModalResult to mrNone won't do unfortunately.
I use BCB, but in the above instance I just return from the function
without setting ModalResult.
 

Re:Check the contents of a dialog before closing

Hi,
You can use the form's OnCloseQuery event
Something like:
procedure TDialogForm.FormClose(Sender: TObject; var CanClose:
boolean);
begin
// first check if user clicked OK or Cancel
if ModalResult=mrCancel then begin
CanClose := true;
exit;
end;
// if user clicked OK, do your checking stuff
if ModalResult=mrOK then begin
if (checking code here) then CanClose := true
else CanClose := false;
end;
end;
Code not tested or compiled.
Cheers,
Chris
 

Re:Check the contents of a dialog before closing

"David Perkins" <XXXX@XXXXX.COM>writes
Quote
>, but my problem is then how to prevent the
>dialog box from actually closing if there are mistakes? To set
>OKBtn.ModalResult to mrNone won't do unfortunately.
I use BCB, but in the above instance I just return from the function
without setting ModalResult.
I also exit the function besides setting OKBtn.ModalResult, but that doesn't
make any difference in my case... the dialog box still closes...
Alan.
 

Re:Check the contents of a dialog before closing

"Chris Morgan" <chris.nospam at lynxinfo.co.uk>writes
Quote
Hi,
You can use the form's OnCloseQuery event
Damn!!! it is always: one tends to forget about the trivial things like
that...
Anyway, thanks for reminding me about it!
Alan... ashamed... :)
 

Re:Check the contents of a dialog before closing

Alan Garny writes:
<snip>
Quote
I also exit the function besides setting OKBtn.ModalResult, but that doesn't
make any difference in my case... the dialog box still closes...

Alan.

I think what you need to do is set the *form's* modalresult, not the
button's modalresult.
HTH
John
 

Re:Check the contents of a dialog before closing

Quote
I also exit the function besides setting OKBtn.ModalResult, but that doesn't
make any difference in my case... the dialog box still closes...
Are you sure you haven't assigned a default ModalResult to the button
beforehand?
 

Re:Check the contents of a dialog before closing

"David Perkins" <XXXX@XXXXX.COM>writes
Quote
>I also exit the function besides setting OKBtn.ModalResult, but that
>doesn't make any difference in my case... the dialog box still closes...
Are you sure you haven't assigned a default ModalResult to the button
beforehand?
By default, it was set to mrOk, yes, so I believe Delphi remembers that, no
matter whether I then modify it or not. This said, I now handle the form's
OnCloseQuery event and it works fine. I had completely forgotten about that
event. Doh!
Alan.