Scrolling a TextBox when form isn't in foreground?

G

Guest

I'm writing a simple debug console application that displays debug messages in a seperate application

The app contains a RichTextBox control to display the text. Vertical scrolling is on. After adding text to the control, I set the text selection to be the last character of the text so that the control will scroll to always display the new text at the bottom of the control

Problem is, the textbox doesn't scroll to the bottom while the console is not the foreground application. New text will be drawn when added (if it's already visible) and the scrollbar updates to show that there's more text in the control. When I bring the console forward, it THEN scrolls to the bottom of the text

I've tried calling textbox.ScrollToCaret(), textbox.Refresh(), and textbox.Update(). None of these help

Can anybody tell me how to get my TextBox control to scroll while in the background

TIA

Steve Johnso
Paris Developmen
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?QmxhdHd1cnN0?= said:
When using a RichTextBox control for displaying logging information, it is
useful to scroll the recently added line into view. There are two ways to
accomplish this:

Untested:

\\\
Private Const WM_VSCROLL As Int32 = &H115
Private Const SB_BOTTOM As Int32 = 7

Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32

Private Sub AddLine(ByVal Destination As RichTextBox, ByVal Text As String)
With Destination
.AppendText(Text & ControlChars.NewLine)
SendMessage(Destination.Handle, WM_VSCROLL, SB_BOTTOM, 0)
End With
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top