Returning to previous focus and cursor location

J

John G.

I have set up a general help popup for a userform. This is just a simple
narration on how to use the input form and therefore context help isn't
needed.
What I would like to do is maintain the cursor position on the original
userform upon closure of the help popup.
The user may want to click on the Help button in the middle of textbox input
and I would like to be able to return to that location when they finish with
the help window.
Since "GetFocus" doesn't seem to exist, I'm not quite sure how to go about
doing this first part.
Second, is the question of being able to not only get back to the current
textbox (I'm assuming "SetFocus" would do tha)t, I would also like to be able
to have the cursor in the same location as when they called up the help
window. Right now it appears to reset to Tab 0.
As always, thanks in advance.
John G.
 
J

Jay Freedman

I have set up a general help popup for a userform. This is just a simple
narration on how to use the input form and therefore context help isn't
needed.
What I would like to do is maintain the cursor position on the original
userform upon closure of the help popup.
The user may want to click on the Help button in the middle of textbox input
and I would like to be able to return to that location when they finish with
the help window.
Since "GetFocus" doesn't seem to exist, I'm not quite sure how to go about
doing this first part.
Second, is the question of being able to not only get back to the current
textbox (I'm assuming "SetFocus" would do tha)t, I would also like to be able
to have the cursor in the same location as when they called up the help
window. Right now it appears to reset to Tab 0.
As always, thanks in advance.
John G.

You'll need to use the Exit procedures of the textboxes to save the
identity of the box being exited and the SelStart and SelLen
properties of that box, putting them into variables declared at the
userform level (before any procedures) so they're available to all
procedures.

Then the code of the Help button, after showing the help, can call
SetFocus on the saved control, and set its SelStart and SelLen
properties back to the saved values.

Private LastBox As TextBox
Private LastSelStart As Integer
Private LastSelLen As Integer

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Set LastBox = TextBox1
LastSelStart = TextBox1.SelStart
LastSelLen = TextBox1.SelLength
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Set LastBox = TextBox2
LastSelStart = TextBox2.SelStart
LastSelLen = TextBox2.SelLength
End Sub

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Set LastBox = TextBox3
LastSelStart = TextBox3.SelStart
LastSelLen = TextBox3.SelLength
End Sub

Private Sub btnHelp_Click()
MsgBox "This is your help"

LastBox.SetFocus
LastBox.SelStart = LastSelStart
LastBox.SelLength = LastSelLen
End Sub

If your userform contains any controls other than textboxes and
buttons, you may want to follow either of two schemes:

- In the exit procedures of the other controls, set LastBox = Nothing.
In the Help button code, surround the three LastBox statements with If
Not LastBox Is Nothing Then .... End If.

- Provide other userform-level variables of the other types (ListBox,
ComboBox, etc.) and use them in a similar manner. The code in the Help
procedure will need to look at each variable in turn to determine
which one of them is Not Nothing.
 
J

John G.

Jay, Thx...this is better than I would have imagined. I will get it
incorporated and get back with the results. Thx again
 
J

John G.

Jay,
Finally got everything finished so I could work on your suggestions and
everything is working just like I wanted to. Figured you had not doubts.
Thx for taking the time on this one.
John G.
 
J

Jay Freedman

You're welcome!

Jay,
Finally got everything finished so I could work on your suggestions and
everything is working just like I wanted to. Figured you had not doubts.
Thx for taking the time on this one.
John G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top