Scroll Richtextbox automatically

D

Daniel von Fersen

Hi,

I have a Funtion

Function addLine(ByVal text As String, ByVal color As Color)

RichTextBox1.SelectionColor = color

RichTextBox1.AppendText("[" + Date.Now.ToShortTimeString + "]: " + text +
vbCrLf)

End Function

that adds a line to my Richtextbox! Know i would like the Richtextbox to
scroll automatically to the end, so you always see the last lines!

U know how to do this?
I would really apperciate that.

Greetz,
Daniel
 
H

Herfried K. Wagner [MVP]

* "Daniel von Fersen said:
Function addLine(ByVal text As String, ByVal color As Color)

RichTextBox1.SelectionColor = color

RichTextBox1.AppendText("[" + Date.Now.ToShortTimeString + "]: " + text +
vbCrLf)

End Function

that adds a line to my Richtextbox! Know i would like the Richtextbox to
scroll automatically to the end, so you always see the last lines!

See my reply in the German language VB.NET group.

Solution: Call the control's 'Focus' and 'ScrollToCaret' methods.
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?QmFycnk=?= said:
There is one problem with using the focus - what if you are on another
form and do not actually want to show the form with the richtextboxwhen
you are appending text, if you do then you cannot use focus, i am
looking for another way to do this for the last few hours haven't found
any, anybody any ideas.

I do not see the post's "parent" post, but maybe this code helps:

\\\
Private Const WM_VSCROLL As Int32 = &H115
Private Const SB_BOTTOM As Int32 = 7

Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32

Private Sub AddLine(ByVal Destination As RichTextBox, ByVal Text As String)
With Destination
.AppendText(Text & ControlChars.NewLine)
SendMessage(Destination.Handle, WM_VSCROLL, SB_BOTTOM, 0)
End With
End Sub
///
 
G

Guest

Thanks Herfried
I manged to find a solution already but it looks similar to yours so both may work, anyway mine i
It uses Sendmessage also but uses Public Const EM_SCROLL = &HB
And SendMessage(RichTextBox1.Handle, EM_SCROLL, 1, 0) - this keeps the cursor at the last line which is what I wanted.
 

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