Link to home
Start Free TrialLog in
Avatar of 3Si_pnewman
3Si_pnewman

asked on

Using SelStart and SelLength

I have a VB6 app in which I am trying to highlight the contents on a textbox.  I am achieving this using the SelStart and SelLength methods however the cursor always ends up on the right hand side of the text.

The problem I have is that if the text is too long for the textbox you cannot see the beginning of the string.  Is there any way to move the cursor to the left of the text after the highlight has taken place?

I have tried doing the SelLength before the SelStart which ends up with the cursor on the left but no text is highlighted.

The code I am using looks like this,

Private Sub highlightText(ByVal ctrlTextBox As TextBox)

    On Error GoTo highlightText_Err

    If Len(ctrlTextBox.Text) <> 0 Then
        ctrlTextBox.SelStart = 0
        ctrlTextBox.SelLength = Len(ctrlTextBox.Text)
    End If

highlightText_Exit:
    Exit Sub
highlightText_Err:
    ....
    Resume highlightText_Exit

End Sub
Avatar of Ravi Singh
Ravi Singh
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi, if the TextBox is not multiline then a quick easy way to do this would be to use the SendKeys procedure to simulate an END keypress followed by SHIFT+HOME (cursor should then be on the left of the highlighted text):

Private Sub highlightText(ByVal ctrlTextBox As TextBox)

    On Error GoTo highlightText_Err

   If Len(ctrlTextBox.Text) <> 0 Then
        ctrlTextBox.SetFocus
        SendKeys "{END}"
        SendKeys "+{HOME}"
    End If

highlightText_Exit:
    Exit Sub
highlightText_Err:
    ....
    Resume highlightText_Exit
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Ravi Singh
Ravi Singh
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 3Si_pnewman
3Si_pnewman

ASKER

I think I'll go for the first answer for now but the same situation could arise in a multiline textbox.

Thanks for your help!