Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Pass through dll 2 38
Data Saving(2) 2 26
Why is "packages element is not declared"? 2 40
Calling stored proc in EDMX 11 19
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

24 Experts available now in Live!

Get 1:1 Help Now