How to Scroll Bottom of RichTextbox into View

C

Charles Law

I have a RichTextbox to which I append text. It is not very tall, so it gets
a scroll bar quite quickly.

When the text is appended, I want the appended text to be visible, but try
as I might, I cannot find a way of programmatically scrolling the control so
that the text at the bottom is visible.

Can anyone tell me how it is done?

TIA

Charles
 
H

Herfried K. Wagner [MVP]

* "Charles Law said:
I have a RichTextbox to which I append text. It is not very tall, so it gets
a scroll bar quite quickly.

When the text is appended, I want the appended text to be visible, but try
as I might, I cannot find a way of programmatically scrolling the control so
that the text at the bottom is visible.

Select the portion of text by calling the overloaded version of the
control's 'Select' method, then call its 'Focus' method followed by a
call to its 'ScrollToCaret' method.
 
C

Charles Law

Thanks Herfried. I have just tried it and that does the job.

Not the first way I would have thought of doing it ;-) Where did you
discover that particular trick? I wouldn't have got there in a month of
Sundays.

Cheers.

Charles
 
H

Herfried K. Wagner [MVP]

* "Charles Law said:
Thanks Herfried. I have just tried it and that does the job.

Not the first way I would have thought of doing it ;-) Where did you
discover that particular trick? I wouldn't have got there in a month of
Sundays.

Some months ago I had the same problem and I found out that scrolling
worked if the RTB had the focus. So I tried setting the focuis to the
control and then calling 'ScrollToCaret'. It worked. Nevertheless, I
think that this is a "bug".
 
G

Guest

You can scroll to the bottom of the RichTextBox without setting focus by going directly to the windows sdk through the dllimport

using:
GetScrollRange(richTextBox1.Handle, SB_HORZ, out min, out max);
SendMessage(richTextBox1.Handle, EM_SETSCROLLPOS, 0, new POINT(max - richTextBox1.Width, 0));

See the following link from syncfusion. It helped me out just now

http://www.syncfusion.com/faq/winforms/search/890.as

-Mike Gol
Microgold Software Inc.
 
C

Charles Law

Thanks Michael

I have just tried it in VB.NET, and GetScrollRange always returns 0 for min
and max.

I am using the following

<DllImport("user32.dll", SetlastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetScrollRange(ByVal hWnd As IntPtr, ByVal nBar As
Integer, ByRef lpMinPos As Integer, ByRef lpMaxPos As Integer) As Boolean
End Function

Does that look right to you?

Charles


Michael Gold said:
You can scroll to the bottom of the RichTextBox without setting focus by
going directly to the windows sdk through the dllimport.
 

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