Solved

Using SelStart and SelLength

Posted on 2006-11-24
3
1,261 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

734 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