Groups | Blog | Home
all groups > dotnet windows forms > june 2005 >

dotnet windows forms : RichTextBox auto scroll to end


Bin Xing
6/27/2005 3:29:04 PM
I'm currently programming a form application in VC++.NET managed code. There
is a RichTextBox and some other controllers on a form. When user is working
on other controllers, like mouse up/down on a picture controller to draw a
picture, I'd like to show some messages on the RichTextBox. I'd like to see
the RichTextBox automatically scrolls to end when new text is appended.
However, during the picture operation, I don't want to lose the focus and
give it to RichTextBox. That means I can't use ScrollToCaret() to move
RichTextBox to the end.

Although TextBox supports AutoScroll and it works perfectly to scroll down
to the end, it seems I can't put too much characters into TextBox. That's the
reason why I have to choose RichTextBox instead of TextBox. But RichTextBox
doesn't support AutoScroll. Someone said I could use Win32 function
SendMessage(), but I don't know how to get current HWnd in VC++.NET managed
code.

Any idea about RichText auto scroll to end? Thanks in advance.

Lloyd Dupont
6/28/2005 12:00:00 AM
[quoted text, click to view]

(HANDLE)(void*) myControl->Handle

AMercer
6/28/2005 4:09:01 AM
Here is a partially tested stab at this problem. It updates and scrolls the
rtb (tested) and then restores focus (untested). I didn't test the restore
focus part because I didn't have a form handy to test with, and it sounds
like you do.

Sample call (Me refers to your form, RichTextBox1 is your rtb):

AppendRtbText(Me.ActiveControl, RichTextBox1, "blah blah" & vbLf)

Sub AppendRtbText:

Public Sub AppendRtbText(ByVal FocusCtl As Control, ByVal Rtb As
RichTextBox, ByVal BottomText As String)
' append text to an rtb, scroll to bottom, and restore focus
With Rtb
.Focus()
.Text &= BottomText
.SelectionStart = .Text.Length
End With
If Not FocusCtl Is Nothing Then FocusCtl.Focus()
End Sub

[quoted text, click to view]
Herfried K. Wagner [MVP]
6/28/2005 8:13:55 AM
"Bin Xing" <BinXing@discussions.microsoft.com> schrieb:
[quoted text, click to view]

Adding a line to a richtextbox and scrolling it into view
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=richtextboxscrolltobottom&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Bin Xing
6/28/2005 11:29:02 AM
I call function SendMessage(), and it works. Thanks.

Bin

[quoted text, click to view]
AddThis Social Bookmark Button