Return Focus After Button Click

Return Focus After Button Click

Here’s an easy way to return the focus to a control after the user clicks on a button on the screen. If you use the keyboard, you can save time this way by not having to tab back to where you were. First, create a module variable of type Control, and call it mCtl. Next, in the GotFocus event of each input control, set mCtl equal to the control that got the focus. When you want to return the focus after the Click event, execute the SetFocus method of mCtl:

 Private mCtl As ControlPrivate Sub Command1_Click()	' perform normal button routine here, then	' return focus to previous control.	On Error Resume Next	mCtl.SetFocusEnd SubPrivate Sub Text1_GotFocus()	Set mCtl = Text1End SubPrivate Sub Text2_GotFocus()	Set mCtl = Text2End Sub
See also  How IoT Tech Will Transform Logistics

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