Using SelLength and SelStart

DCrake

Remembered
Local time
Today, 20:29
Joined
Jun 8, 2005
Messages
8,626
Cany anyone shed light on why this code does not work?

Code:
Private Sub GetChar(CtrlName As String)
    Dim Ctrl As Control
    Set Ctrl = Me(CtrlName)
    
    txtUpChar.SetFocus
    If txtUpChar.SelStart = 0 Or txtUpChar.SelStart = Len(txtUpChar.Text) Then
        txtUpChar.Text = txtUpChar.Text & Ctrl.Caption
    ElseIf txtUpChar.SelLength = 0 Then
        txtUpChar.Text = Mid(txtUpChar.Text, 1, txtUpChar.SelStart) & _
        Ctrl.Caption & Mid(txtUpChar.Text, txtUpChar.SelStart + 1)
    Else
        txtUpChar.Text = Mid(txtUpChar.Text, 1, txtUpChar.SelStart) & _
        Ctrl.Caption & _
        Mid(txtUpChar.Text, txtUpChar.SelStart + txtUpChar.SelLength + 1)
    End If
    txtUpChar.SelStart = Len(txtUpChar.Text)
    

End Sub

This code is fired on the on click of a button. The issue is that the SelStart always returns zero not the actual position in the string where the curson is.
It works fine in VB but fails in Access.

It seems that when a textbox looses focus by way of clicking on a button when the code passes back the focus to the textbox the cursor is always at the start of the text. This may have something to do with the behaviour entering field setting, don't know.
 
When you click out of the text box, you would lose the starting position. So you will need, in the Lost Focus event to set a variable to the position so you can come back to it.
 
Hi,
I have a similar problem. I'm trying to use the selstart property to retrieve the cursor position in a textbox on a form so that when I return focus to the textbox, I can set the cursor position back to where it was before the textbox lost focus. The problem is, the selstart property returns as 0 every time even if the cursor is not at the 0 position. I also as a curiosity looked at the sellength property and found that this was always the length of the text in the textbox even when none of it is selected. This seems to imply the whole text in the textbox is selected but it doesn't show up that way. The code is being run from the textbox_change event. Any ideas why this is and how I might get around it?

Here is the line of code in question.

lCurPos = Forms!library!SearchBox.SelStart

Thanks for your help!
Chris
 
How, exactly, are you testing to determine the cursor position?

...I'm trying to use the selstart property to retrieve the cursor position in a textbox on a form so that when I return focus to the textbox, I can set the cursor position back to where it was before the textbox lost focus...
In answer to your immediate problem, I ran this up for someone a while back. Basically you need to set up a Variable to keep track of the cursor position and then use it when you return Focus to the Control to set the start position:
Code:
Option Compare Database
Option Explicit

Dim varControlName As Integer

Private Sub Form_Current()
 varControlName = 0
End Sub


Private Sub ControlName_Change()
 varControlName = varControlName + 1
End Sub

Private Sub ControlName_GotFocus()
 Me.ControlName.SelStart = varControlName
End Sub
Linq ;0)>
 
Misslinglinq, thanks for your reply. Essentially that is exactly what I'm doing with regards to variables. I have no issue resetting the cursor value to some arbitrary position after returning focus to the text box in question and had the code working using the string length as the new cursor position. The issue with both my approach and yours is that it doesn't take account of whether the text is being edited in the middle of the string. It will always replace the cursor at the end of the string which is not the behaviour I'm after. I ideally want the cursor to be after the character just typed wherever in the string that is. To this end, I assumed that you could read the selstart property to get the position of the cursor but it doesn't seem to be working. Any further ideas?
 
I've just realised what my problem was! It hadn't occurred to me that a line of code before that was using the .text property to get the text string was in fact selecting the text...durr. By putting the cursor position getting code in before the code that selects the text, my problem is now solved!
 
Glad to hear that you solved it! Could you post your final code? Others may pass by this thread, at a later date, as you did, with similar problems. Your solution could help them, as well!

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom