Solved

How to auto-scroll to bottom of RichTextBox when it doesn't have focus

Posted on 2004-04-30
5
3,253 Views
Last Modified: 2010-02-11
OK, so here's the deal: I am writing an application that reads a network stream asyncronously and updates a RichTextBox by firing a custom event. That part works well, except that I need the RichTextBox to automatically scroll down to show the newly added text (in many respects this application functions as a simple telnet client). Any idea how to do this?

I have tried setting the SelectionStart and SelectionLength properties, and I have tried calling the ScrollToCaret() method--all of which require the RichTexBox to have the current focus. I can't give the RichTextBox focus because the TextBox control that handles user commands needs to have focus so the user can interact with the server in realtime.

So to summarize, I need the RichTextBox control to auto-scroll to the bottom line of text each time a new line is added to the control--without giving the control focus.

Thanks for you suggestions.
0
Comment
Question by:Bill_Manning
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10966205
Add this code to the form which contains rich text box:

using System.Runtime.InteropServices;

namespace ...
{
     public class Form1 : System.Windows.Forms.Form
     {
         ....

        [DllImport("user32.dll")]
        public static extern int PostMessage(IntPtr wnd, uint Msg, IntPtr wParam, IntPtr lParam);
        public const uint WM_VSCROLL = 0x0115;
        public const uint SB_BOTTOM = 7;


To scroll rich text box to the end from any place, write:

PostMessage(richTextBox1.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, (IntPtr)IntPtr.Zero);
0
 
LVL 11

Expert Comment

by:Agarici
ID: 10979983
you could also try this:


richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
0
 

Author Comment

by:Bill_Manning
ID: 10980185
AlexFM,

Your solution works perfectly; but before I close the question, I wonder if you could help me understand what it's doing. It looks like a similar technique would solve several other problems I'm having with this RichTextBox, and I would like to know more about it.  An explanation of your code and some URLs or books would be very helpful. In any case, I'll be accepting your answer.

Thanks very much.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10983575
0
 

Expert Comment

by:Richard Payne
ID: 26552225
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();

Seem to work very well, can't see reason for using more complex solution.

Is there practice to avoid interop or good reason to avoid them?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

19 Experts available now in Live!

Get 1:1 Help Now