Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-30
5
Medium Priority
?
3,896 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 2000 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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

824 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