RichTextBox auto scroll to end

G

Guest

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.

Bin
 
G

Guest

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
 
L

Lloyd Dupont

but I don't know how to get current HWnd in VC++.NET managed code.

(HANDLE)(void*) myControl->Handle
 

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