Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto-scrolling text box 1

Status
Not open for further replies.

Motor11

Technical User
Jul 30, 2002
60
US
Hey all,

I've written an app with a textbox that I hope to have scroll to the bottom as I add information to it (a terminal window that shows processing information).

All the info I can find on this issue says to add

text1.selstart=len(text1.text)

and possibly

text1.selLength=0

This does not work. I still have to scroll the textbox manually, at which time I can see the selection carrot at the bottom of the textbox. Is there a way to have the textbox scroll with text as it is added? Do I possibly have to change on of this textbox's properties?
 
Study this code it should suit your purpose.
test by adding textbox and timer to new project

Code:
Private Sub Form_Load()
    'for text1 remember to set the scrollbar property to vertical and multiline property to true
End Sub

Private Sub Timer1_Timer()
    Dim lPos As Long
    Dim pt As POINTL
    Dim r As RECT
    Dim lCount As Long
    Dim sTemp As String
    Dim l As Long

    sTemp = String$(Rnd() * 5 + 5, Rnd() * 26 + Asc("A")) & vbCrLf
    With Text1
        lCount = SendMessage(.hwnd, EM_GETLINECOUNT, 0, ByVal 0&) - 1
        GetClientRect .hwnd, r
        pt.x = r.Left + 1
        pt.y = r.Bottom - 1
        lPos = SendMessage(.hwnd, EM_CHARFROMPOS, 0, pt)
        lPos = SendMessage(.hwnd, EM_LINEFROMCHAR, lPos, ByVal 0&)
        If lPos < lCount Then 'do not scroll
            l = SendMessage(.hwnd, EM_GETSCROLLPOS, 0, pt)
            .Text = .Text & sTemp
            l = SendMessage(.hwnd, EM_SETSCROLLPOS, 0, pt)
        Else
            .Text = .Text & sTemp
            .SelStart = Len(.Text)
        End If
    End With
End Sub
 
missed some code here is the whole copy

Code:
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_USER = &H400
Private Const EM_GETSCROLLPOS = (WM_USER + 221)
Private Const EM_SETSCROLLPOS = (WM_USER + 222)
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_CHARFROMPOS = &HD7
Private Const EM_GETLINECOUNT = &HBA

Private Type POINTL
    x As Long
    y As Long
End Type

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Sub Form_Load()
    'remember to set the scrollbar property to vertical and multiline property to true
End Sub

Private Sub Timer1_Timer()
    Dim lPos As Long
    Dim pt As POINTL
    Dim r As RECT
    Dim lCount As Long
    Dim sTemp As String
    Dim l As Long

    sTemp = String$(Rnd() * 5 + 5, Rnd() * 26 + Asc("A")) & vbCrLf
    With Text1
        lCount = SendMessage(.hwnd, EM_GETLINECOUNT, 0, ByVal 0&) - 1
        GetClientRect .hwnd, r
        pt.x = r.Left + 1
        pt.y = r.Bottom - 1
        lPos = SendMessage(.hwnd, EM_CHARFROMPOS, 0, pt)
        lPos = SendMessage(.hwnd, EM_LINEFROMCHAR, lPos, ByVal 0&)
        If lPos < lCount Then 'do not scroll
            l = SendMessage(.hwnd, EM_GETSCROLLPOS, 0, pt)
            .Text = .Text & sTemp
            l = SendMessage(.hwnd, EM_SETSCROLLPOS, 0, pt)
        Else
            .Text = .Text & sTemp
            .SelStart = Len(.Text)
        End If
    End With
End Sub
 
Text1.SelStart = 65535

However, text1.selstart=len(text1.text) should indeed work (it's just fractionally slower than my variant), so it seems like something else is going on that is preventing it from doing so.

I think we need to see your code
 
Thanks for the responses.

three57m's code works well. I'll have a closer look at it to see how to make use of it. Not sure why the simpler solution does not work. As for my code, it's pretty simple:

Code:
Private Sub txtStationTerm_Change()
    With txtstationterm
        .SelStart = Len(.Text)
        .SelLength = 0
    End With
End Sub

I also tried:

Code:
Private Sub txtStationTerm_Change()
    With txtstationterm
        .SelStart = Len(.Text)-1
        .SelLength = 1
    End With
End Sub

With this code, I can see the the last character in the textbox is indeed selected. However, I still need to scroll manually to see the highlighted character. Not sure why.

I've also tried setting the SelStart property in the procedure where I write to the textbox. Still no scrolling. Same result if I use SelStart = 65535.

Thanks for the help.
 
I just had a breakthrough!

The code to write to the textbox was being called from my form's load event. Problem was that the textbox wasn't visible yet. Simply had to add a "me.show" statement prior to writing to my textbox so that it was actually drawn first.

Now the simple .SelStart method works. HughLerwill's suggestion was very helpful as the .SetFocus statement failed and I had to investigate why. That's how I stumbled across the solution.

So thanks all.
 
As I said : something else [was] going on that is preventing it from doing so. Glad you've managed to find out what.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top