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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
Thanks for your help!
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