Solved

Using SelStart and SelLength

Posted on 2006-11-24
3
1,262 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:3Si_pnewman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 18007427
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
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 125 total points
ID: 18007635
For a MultiLine textbox you could use the SendMessageAsLong API function to get the line count of the textbox then simulate SHIFT + UP arrow keypresses, the following should work OK for both single line and multi line textboxes:

'Declare globally
Private Declare Function SendMessageAsLong Lib "user32" _
     Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const EM_GETLINECOUNT = &HBA


'Following function returns the line count given a textbox
Private Function GetLineCount(ByVal TB As TextBox) As Long
    GetLineCount = SendMessageAsLong(TB.hWnd, EM_GETLINECOUNT, 0, 0)
End Function


'The selection code
Private Sub highlightText(ByVal ctrlTextBox As TextBox)

    On Error GoTo highlightText_Err

        ctrlTextBox.SetFocus
        ctrlTextBox.SelStart = Len(ctrlTextBox.Text)
        SendKeys "+{HOME}"
       
        If ctrlTextBox.MultiLine Then
            Dim i As Integer
            For i = 1 To GetLineCount(ctrlTextBox) Step 1
                SendKeys "+{UP}"
            Next
        End If

highlightText_Exit:
    Exit Sub
highlightText_Err:
    ....
    Resume highlightText_Exit
End Sub
0
 
LVL 1

Author Comment

by:3Si_pnewman
ID: 18007727
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Make the most of your online learning experience.
Simple Linear Regression
Progress

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question