devxlogo

Trapping Cancel Button

Trapping Cancel Button

Question:
In a simple loop on a form with a Cancel Button:

DoHere I want to Check to see if a user pushed a cancel button(Cancel Property is set to true)(While in loop can’t get focus to Cancel Button to see if it was pushed. Setting focus to it does not work. There must be a simple way to trap for this) If Cancel is Pushed, Exit LoopLoop
Any info would be appreciated.

Answer:
What I would do is from the Cancel_Click event, set a flag to True. At the bottom of your loop put a DoEvents statement. This frees up the processor to check for things like button clicks, etc. At the top of your loop, check the flag and if True, exit the loop. Let’s say your flag is global and is called gintCancelFlag. Pseudo-code looks something like this:

DoIf gintCancelFlag = True Then Exit Do’In here you’d put the code the loop is supposed to be doing.DoEvents ()  ‘note that the () is necessaryLoop

See also  5 Tips for Working With an Onsite Interpreter
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist