Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

TextBox: autoscrolling to end after AppendText

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Arkion - 19 Sep 2004 10:20 GMT
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);
Jared - 19 Sep 2004 13:26 GMT
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
///

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<<<Herfried K. Wagner [MVP]>>>

> Hello!
> How would I make a TextBox control auto-scroll to bottom after calling
[quoted text clipped - 6 lines]
> textBox1.AppendText(val);
> textBox1.Select(control.TextLength, 0);
Arkion - 19 Sep 2004 14:47 GMT
Greg Dunn - 19 Sep 2004 15:15 GMT
Try this:

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

Greg Dunn

> Hello!
> How would I make a TextBox control auto-scroll to bottom after calling
[quoted text clipped - 6 lines]
> textBox1.AppendText(val);
> textBox1.Select(control.TextLength, 0);
Greg Dunn - 19 Sep 2004 16:13 GMT
Sorry, that last post should have read:

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

Greg Dunn

> Try this:
>
[quoted text clipped - 14 lines]
> > textBox1.AppendText(val);
> > textBox1.Select(control.TextLength, 0);
Imran Koradia - 19 Sep 2004 15:56 GMT
How about using the ScrollToCaret Method?

Imran.

> Hello!
> How would I make a TextBox control auto-scroll to bottom after calling
[quoted text clipped - 6 lines]
> textBox1.AppendText(val);
> textBox1.Select(control.TextLength, 0);
Jared - 19 Sep 2004 23:15 GMT
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

> How about using the ScrollToCaret Method?
>
[quoted text clipped - 10 lines]
>> textBox1.AppendText(val);
>> textBox1.Select(control.TextLength, 0);
Greg Dunn - 21 Sep 2004 21:02 GMT
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

> 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
[quoted text clipped - 17 lines]
> >> textBox1.AppendText(val);
> >> textBox1.Select(control.TextLength, 0);
Jared - 21 Sep 2004 23:35 GMT
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."

> Both
>
[quoted text clipped - 32 lines]
>> >> textBox1.AppendText(val);
>> >> textBox1.Select(control.TextLength, 0);
Greg Dunn - 22 Sep 2004 05:39 GMT
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

> Greg,
> As I said before, I too believed this, until 2 MVP's set me strait. If the
[quoted text clipped - 44 lines]
> >> >> textBox1.AppendText(val);
> >> >> textBox1.Select(control.TextLength, 0);
Jared - 24 Sep 2004 11:54 GMT
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

> Jared:
>
[quoted text clipped - 84 lines]
>> >> >> textBox1.AppendText(val);
>> >> >> textBox1.Select(control.TextLength, 0);
Greg Dunn - 28 Sep 2004 01:30 GMT
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

> Greg,
>     It seems as though the RichTextBox acts differently that a TextBox.
[quoted text clipped - 90 lines]
> >> >> >> textBox1.AppendText(val);
> >> >> >> textBox1.Select(control.TextLength, 0);

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.