Solved

Using SelStart and SelLength

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

809 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