devxlogo

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

devx-admin

Share the Post: