Solved

Using SelStart and SelLength

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 109
Java Loop 6 67
How to get time difference in minutes and seconds only between 2 dates 2 46
statistics basics sources 2 12
A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

820 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