Set cursor position back to textbox

  • Thread starter nigelb via AccessMonster.com
  • Start date
N

nigelb via AccessMonster.com

Ok, I have an interesting question about textboxes for anyone up for the
challenge...

I have a multi-line textbox containing text that the user can scroll through
with the vertical scroll bars. When the user clicks a command button, the
textbox loses focus (understandably).

Is there a way to set focus back to a textbox (after the user click a command
button) so that it clears text selection and it retains the same cursor and
scroll bar position before it lost focus?

My code is...

Private Sub txtLicense_GotFocus()
txtLicense.SelLength = 0
End Sub

This successfully sets focus back to the textbox and clears the text
selection, but the cursor position is returned back to the top at the first
character again (and so the previous cursor and scroll position is lost).

Any ideas?
 
D

Dirk Goldgar

nigelb via AccessMonster.com said:
Ok, I have an interesting question about textboxes for anyone up for the
challenge...

I have a multi-line textbox containing text that the user can scroll
through
with the vertical scroll bars. When the user clicks a command button, the
textbox loses focus (understandably).

Is there a way to set focus back to a textbox (after the user click a
command
button) so that it clears text selection and it retains the same cursor
and
scroll bar position before it lost focus?

My code is...

Private Sub txtLicense_GotFocus()
txtLicense.SelLength = 0
End Sub

This successfully sets focus back to the textbox and clears the text
selection, but the cursor position is returned back to the top at the
first
character again (and so the previous cursor and scroll position is lost).


I don't know about the scroll-bar position, but the cursor (caret) location
can probably be restored. I haven't tried this, but I would think that you
could use the control's LostFocus event to capture the current value of
..SelStart into a module-level variable, and then when you want to return the
focus to that control and position, set the .SelStart to the preserved
value. Like this:

Dim mlngSelStart As Long

Private Sub txtLicense_LostFocus()

mlngSelStart = Me.txtLicense.SelStart

End Sub

Private Sub cmdYourButton_Click()

With Me.txtLicense
.SetFocus
.SelStart = mlngSelStart
End With

End Sub
 
M

Marshall Barton

nigelb said:
Ok, I have an interesting question about textboxes for anyone up for the
challenge...

I have a multi-line textbox containing text that the user can scroll through
with the vertical scroll bars. When the user clicks a command button, the
textbox loses focus (understandably).

Is there a way to set focus back to a textbox (after the user click a command
button) so that it clears text selection and it retains the same cursor and
scroll bar position before it lost focus?


Assuming the text box is named Remarks, try using this kind
of code:

Private pos As Long

Private Sub Remarks_GotFocus()
Me.Remarks.SelLength = 0
Me.Remarks.SelStart = pos
End Sub

Private Sub Remarks_LostFocus()
pos = Me.Remarks.SelStart
End Sub
 
N

nigelb via AccessMonster.com

Pefect!! Thanks.

Dirk said:
Ok, I have an interesting question about textboxes for anyone up for the
challenge...
[quoted text clipped - 20 lines]
first
character again (and so the previous cursor and scroll position is lost).

I don't know about the scroll-bar position, but the cursor (caret) location
can probably be restored. I haven't tried this, but I would think that you
could use the control's LostFocus event to capture the current value of
.SelStart into a module-level variable, and then when you want to return the
focus to that control and position, set the .SelStart to the preserved
value. Like this:

Dim mlngSelStart As Long

Private Sub txtLicense_LostFocus()

mlngSelStart = Me.txtLicense.SelStart

End Sub

Private Sub cmdYourButton_Click()

With Me.txtLicense
.SetFocus
.SelStart = mlngSelStart
End With

End Sub
 
N

nigelb via AccessMonster.com

Thanks, this works well.

Marshall said:
Ok, I have an interesting question about textboxes for anyone up for the
challenge...
[quoted text clipped - 6 lines]
button) so that it clears text selection and it retains the same cursor and
scroll bar position before it lost focus?

Assuming the text box is named Remarks, try using this kind
of code:

Private pos As Long

Private Sub Remarks_GotFocus()
Me.Remarks.SelLength = 0
Me.Remarks.SelStart = pos
End Sub

Private Sub Remarks_LostFocus()
pos = Me.Remarks.SelStart
End Sub
 

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