TextBox: autoscrolling to end after AppendText

A

Arkion

Hello!
How would I make a TextBox control auto-scroll to bottom after calling
AppendText? I have tried various methods but none of them seem to
work. This is already tried:

textBox1.HideSelection = false; // Called from Init method
..
..
textBox1.AppendText(val);
textBox1.Select(control.TextLength, 0);
 
J

Jared

Herfried Wagner has your answer in the microsoft.public.dotnet.language.vb
newsgroup post dated 26 June 2004, titled Re: TextCursor in RichTextBox. You
need to used an unmanaged API call; any thanks goes to him.

<<<Herfried K. Wagner [MVP]>>>
If you want to scroll to the end of the text:

From my FAQ:

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:

\\\
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
///
 
G

Greg Dunn

Try this:

textBox1.AppendText(val)
textBox1.SelectionStart = pTextBox.Text.Length
textBox1.Focus()

Greg Dunn
 
G

Greg Dunn

Sorry, that last post should have read:

textBox1.AppendText(val)
textBox1.SelectionStart = textBox1.Text.Length
textBox1.Focus()

Greg Dunn
 
J

Jared

I think I suggested something like this when I first encountered a question
about scrolling to the newly appended text in a textbox/richtextbox; the
advantage of the method the Herfried proposed is that the control does not
need the focus, which will involve a lot less code in the long run. This
sold me on the api call vs. using the built in methods.
Jared
 
G

Greg Dunn

Both

Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length

and

Me.TextBox1.ScrollToCaret()

work without the targetted control either needing the focus, or getting it
as a result.

Greg Dunn
 
J

Jared

Greg,
As I said before, I too believed this, until 2 MVP's set me strait. If the
control doesn't have the focus then this method won't work. That why I made
the original suggestion. If you read the post that I mentioned earlier you
will see that I was arguing the same thing as you, and, as it turned out. We
were/are wrong. The following excerpt is from the previously reference post.

Jared

"Yes you are right the richtextbox needs focus for the scrolltocaret to
work."
 
G

Greg Dunn

Jared:

Before I posted the last message I built a Windows form to test it. The form
has two textboxes and a button. TextBox txtOutput has its Multiline
property set to True, and its Text property contains a string sufficiently
long so as to overrun the depth of the box. TextBox txtInput is just a
regular textbox that starts with a string about the length of an average
sentence.

Here's the code for the button:

Private Sub btnAppendText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAppendText.Click
Me.txtOutput.AppendText(Me.txtInput.Text)
Me.txtOutput.ScrollToCaret()
End Sub

Upon clicking that button, txtOutput is scrolled to the bottom of the text,
and the button still has the focus. Same result if you substitute the
statement

Me.txtOutput.SelectionStart = Me.txtOutput.Text.Length

for the ScrollToCaret() line.

Try it, and let me know if you get a different result.

Greg
 
J

Jared

Greg,
It seems as though the RichTextBox acts differently that a TextBox.
You're right, a textbox doesn't need the focus, try your same routine with a
RichTextBox control. Do you have the same results? Does it scroll?
Jared
 
G

Greg Dunn

I was still responding to Arkion's original question, which asked how to
make a TextBox (not a RichTextBox) control auto-scroll to bottom after
calling
AppendText. But you're correct, the RichTextBox behaves differently. For
my part, I'd still handle the situation without an unmanaged API call -- I
guess I just don't like them buggers, so if I can avoid them and get what I
want, I do. All you have to do is to set the focus momentarily to the
RichTextBox after appending the text to it, then restore the focus to the
button (or whatever it was) you used to do the append. And of course, the
setting and restoring of the focus could be wrapped neatly in a procedure if
you were doing a lot of this.

Greg
 

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