Solved

Using SelStart and SelLength

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
Excel object stays open 19 65
sumDigits  challenge 7 60
word0 challenge 4 52
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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now