Using LostFocus Events with Command Button Properties

Using LostFocus Events with Command Button Properties

If you’re using LostFocus events but would also like to set the Defaultor Cancel properties for command buttons, you’ll run into a confusing situation.Those properties trigger the Click event when the user presses either Enteror Escape without ever transferring focus to the command button.To trigger the “missing” LostFocus, you need to explicitly transferfocus yourself, and then call DoEvents to resequence to a VB chain of events.(Don’t be scared by the naysaying over DoEvents–it’s extremely usefulin this situation and can cause no ill effects!) Use code similar to this:

 Sub Command1_Click () Command1.SetFocus DoEvents If Not (ActiveControl Is Command1) Then 'Focus was transfered elsewhere 'by validation code Exit Sub Else 'Proceed with Click event code End If End Sub 
See also  Why ChatGPT Is So Important Today

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