Prevent textbox scrolling

K

Kent Briggs

VB 2005 Express. I have a readonly multiline textbox with a vertical
scrollbar. I'm programatically adding text to it with
textbox1.appendtext(mytext) and it scrolls automatically. However, I
only want automatic scrolling when the thumb control is at the bottom
position. If the user scrolls up, I want it to hold its position as more
text is appended.

So basically I need to know:

1. How to detect if the scrollbar is at bottom position

2. How to append text without scrolling the textbox.
 
M

Michel van den Berg

Dear Kent Briggs,

Try this:

SetScrollPos(Me.TextBox1.Handle,1 , Me.TextBox1.Lines.Length - 1, True)
SendMessage(Me.TextBox.Handle, EM_LINESCROLL, 0, Me.TextBox1.Lines.Length -
1)

where EM_LINESCROLL is a constant integer and its value = 0x00B6
Please do look this value up (and the SendMessage declaration if wanted) for
yourself in MSDN library.

Hope this helps,

Michel van den Berg
 
K

Kent Briggs

Michel said:
Dear Kent Briggs,

Try this:

SetScrollPos(Me.TextBox1.Handle,1 , Me.TextBox1.Lines.Length - 1, True)
SendMessage(Me.TextBox.Handle, EM_LINESCROLL, 0, Me.TextBox1.Lines.Length -
1)

Thanks, I finally got it to work using this code:

Private Sub UpdateTextBox(ByVal s As String)
' get current position
Dim startpos As Integer = GetScrollPos(TextBox1.Handle, SBS_VERT)
' scroll to bottom
SetScrollPos(TextBox1.Handle, SBS_VERT, TextBox1.Lines.Length - 1,
True)
' get bottom position
Dim bottompos As Integer = GetScrollPos(TextBox1.Handle, SBS_VERT)
' append new text
TextBox1.AppendText(s & vbCrLf)
' restore original position
If startpos <> bottompos Then
PostMessageA(TextBox1.Handle, WM_VSCROLL, SB_THUMBPOSITION +
&H10000 * startpos, Nothing)
End If
End Sub
 
M

Michel van den Berg

Dear Kent Briggs,

Great to hear and thanx for sharing the code!

Michel van den Berg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top