1 (edited by joshuA 2022-05-21 13:52:30)

Topic: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Hi Folks!
.
I could use some help here, and I hope the topic is self-explanitory.
.
I'm not able to troubleshoot this because I don't fully understand what's going on in this line:
.

Form2.OnClose := @Form2_OnClose;

.
I've attached a sample project for demonstration...
.
There are currently two unsolved issues:
.
1. When the user changes a field, and then reverts the field back to the original content, the confirmation does not show AND the form will not close (or respond) either.
.
2. The confirmation is showing even when the user clicks the save button.
.
Originally I was using the Close button's On_Click event to handle this.  Then I noticed the user could simply click the window's X (close) button without any confirmation.  So then I found this thread that would handle that, but I can't get it working properly.
.
At this point, I'm almost ready to just settle with the first approach.  I would appreciate any advice and/or suggestions.

Post's attachments

Attachment icon confirmation-dialog.zip 327.15 kb, 143 downloads since 2022-01-25 

"Energy and persistence conquer all things."

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Hi,
For verification, you can use the Form2_btnClose_OnClick event and not display the X button.

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Hi sparrow,
.
Okay, that's another option I didn't think of... haha!
.
Thank you for contributing my friend.

"Energy and persistence conquer all things."

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Also sparrow... since you contributed the first solution in that thread I referenced, would you mind further explaining or giving me somewhere else to look so I can learn what you're doing in that statement:
.

Form1.OnClose := @frm1oncl;

.
What is the @ symbol doing there?  This is over my head, and I'm curious to learn it.
.
On first glance, it seems like it may be creating an instance of the function or something..?

"Energy and persistence conquer all things."

5 (edited by sparrow 2022-01-25 21:29:08)

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

yes

and your prog with X

Post's attachments

Attachment icon conf-d.zip 335.48 kb, 179 downloads since 2022-01-25 

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Brilliant!
.
Precisely what I was after... even though I don't fully understand it.  I'm guessing that's just the syntax for calling a custom function/procedure.
.
Many thanks!

"Energy and persistence conquer all things."

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Hi Joshua, Hi Sparrow,
Not quite - the '@' isn't required when you use your own procedures;  however, the '@' IS required as a prefix when using one of your own written procedure as a replacement for the standard one associated with a particular action (such as 'formclose' or selecting a menu options).
Have a look at the attached simple example which replaces the existing 'about' option with your own procedure ('aboutme'). 
Note how it can be called in 2 different ways.
1.  from a normal button so can be referenced directly as 'aboutme' with no need for '@'.
2.  as a replacement for the pre-existing MVD menu option and so has to be referenced as '@aboutme'.
Hope that clarifies rather than confuses matters!
Derek.

Post's attachments

Attachment icon aboutwindow.zip 351.57 kb, 237 downloads since 2022-01-26 

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Hi derek,
.
I'm starting to get the idea, and I appreciate the extra explanation.  I've seen that structure used in threads about customizing menus, but it was all new to me.  I wanted to ask about it here since this was a little more simple use.
.
Thank you both again.

"Energy and persistence conquer all things."

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Hello again,

I have a few other related questions to add for this thread...


1. Is it possible to disable the close button on the MessageDlg() window?  And if not, is it possible to set which button to used as the default (for when the dialog is closed in this way)?


For example:

I noticed that MVD has a confirmation dialog whenever you Clear a file from an DBFile component.  And it will not allow the user to close that dialog without clicking one of the buttons.

Using the MessageDlg(), the user can still close the dialog without clicking one of the buttons which disrupts the script that was provided as a solution in this thread.

If it's not possible to disable the close button, is there a way to set or capture a default action when the dialog is closed?  Anyone have any ideas for how to handle this?


2. Does anyone know of a DialogType parameter for the MessageDlg() to show the question mark similar to the one used in the MVD dialog mentioned above?

The only list mentioned in this thread that I was able to find was from back in 2015.  Is there one available now?

According to the Delphi Basics reference, the mtConfirmation "Displays an question mark".  But, this isn't the case for MVD.  Does anyone know of one for this?

---

I have a screenshot to illustrate my questions...

Thank you for any feedback!

Post's attachments

Attachment icon question-09_7977.png 28.76 kb, 77 downloads since 2022-01-30 

"Energy and persistence conquer all things."

10 (edited by sparrow 2022-01-30 11:58:38)

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

--- POST edited ----
You can use MessageBox... (https://delphiexample.at.ua/publ/metod_ … x/1-1-0-27) - rus

procedure Form2Cl (Sender: TObject; Action: TCloseAction);
begin
    if ((ff01 <> Form2.tbName.sqlValue) OR
    (ff02 <> Form2.dtpDate.sqlDate)) AND NOT (Form2.btnSave.Focused) then

       if MessageBox('YOUR MESSAGE','YOUR LABEL',MB_OKCANCEL + MB_ICONQUESTION + MB_DEFBUTTON2) =  IDCANCEL then Action := caNone;

end;

if you use this code X dont work, becouse no button Cancel

procedure Form2Cl (Sender: TObject; Action: TCloseAction);
begin
    // Check for changes
    if ((ff01 <> Form2.tbName.sqlValue) OR
    (ff02 <> Form2.dtpDate.sqlDate)) AND NOT (Form2.btnSave.Focused) then

       if MessageBox('YOUR MESSAGE','YOUR LABEL',MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2) =  IDNO then Action := caNone;
end;

Re: [SOLVED] Prompt User Before Closing Form(s) When Fields Have Changed

Hi sparrow,

I haven't been able to experiment with it yet, but it looks like that covers both of my questions.

Thank you for sharing that site.

"Energy and persistence conquer all things."