Solved

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

Posted on 2004-04-30
5
3,593 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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