Solved

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

Posted on 2004-04-30
5
3,476 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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set custom font WPF 2 25
Stop Git from being my repository 1 49
C# rename a class and update all references efficiently 6 53
C# LINQ query question 11 32
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a feā€¦

738 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