Cancel button in Delphi VCL to over-ride default action in program

Posted on 2013-10-04
Medium Priority
Last Modified: 2013-10-11
I have OnExit actions on many window components, as the user works through the screen.

However, if users want to stop processing, and they click on the cancel button or the exit button, the action of that button won't take place until the users get to a field that has no OnExit event (or similar). In this case the users have to keep clicking until they get the required response.

I have worked out a solution that seems to work, which is make the OnExit events indirect, by using a timer event to delay the execution of these OnExit events, which lets the Cancel button get the first action (and therefore lets me set a StopProcessing flag).

Is that the standard approach, or is there a better solution?
Question by:GrahamDLovell
  • 4
  • 3
LVL 28

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 750 total points
ID: 39545858
Better solution is to use Forms OnCloseQuery event. If you put on some button Close to close form, then OnCloseQuery is fired up. There you can check for is all done:


procedure Tform1.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
  bOK: Boolean;
  CanClose := True; //enable close
  if not Check4JobDone then
    CanClose := False; //show some message here if want....

Open in new window

....but using TAction on each Button is more useful ...
With Actions use OnUpdate event to disable some of buttons and disable it until some conditions are fulfilled
LVL 38

Assisted Solution

by:Geert G
Geert G earned 750 total points
ID: 39545991
it's difficult to follow what you mean

is this like an installation process with next, previous, finish and exit ?
when clicking cancel instead of next, there is usually a question
  > Do you want to exit the installation

how the app reacts depends on how it's coded

Author Comment

ID: 39546130

It is not like an installation process. It is more like completing a complicated form, but allowing the user to give up part way through the process.

I set the form up as two parts. The controls for the first part are on a panel. The second part is a TStringGrid on the form itself. Moving from the panel to the Grid involves running some checks that the first part data is complete, and then locating a combobox on the Grid (logically like the standard oncloseQuery, suggested above, but not on the close of the form).

I am mostly just using the tab order to move from one field to the next, but when I click on Cancel it just moves to the next field in the panel. On reflection (and after checking the code again to make sure I wasn't forcing this happen) this seems weird, so I looked further into my problem.

I can now see that part of my problem is in my use of a panel to hold the first set of controls, since putting the Cancel button on the panel avoids that symptom.

I can probably fix that part of the problem by getting rid of the panel (not as elegant, but workable). The TStringGrid issue is more complicated, since some fields are user controlled, and some programmatically controlled. I can use a timer here when moving from one cell to another, if necessary, delaying the action for OnSelectCell.
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.


Accepted Solution

GrahamDLovell earned 0 total points
ID: 39548107
OK. Here is my own solution.

The problem was of my own making.

Firstly, I used a panel to assist in handling the navigation. It didn't help, and when I removed it, the navigation improved.

Secondly, my field validation was poorly placed, since I refused to allow the program to exit a field unless it was properly completed. This meant that no other button was allowed to take any action on such a field. When I deferred the validation, it solved that problem.

Thanks to both those who responded. You both made me think about the underlying difficulties.
LVL 38

Expert Comment

by:Geert G
ID: 39551876
it looks like the problem was in the code...glad we could help ... a little :)

Author Closing Comment

ID: 39558320
Neither of the expert responses provided an actual solution to my problem, but both helped me to solve it.
LVL 38

Expert Comment

by:Geert G
ID: 39564688
ask a blind man to drive your car and then complain it's got a dent

same as asking to help with your code which you didn't share

Author Comment

ID: 39564791
I wasn't aware that I had made a complaint: I was just stating the facts.

I appreciated the help I received.

My request really was of the sort "why is it so difficult to make this functionality work", and I interpreted both of the responses I received as saying "this should be simple."

This sent me back to the code to develop a test case, and in doing this, I found several problems that I had created.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

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…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses

627 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