Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

On a multi-line text box I'm appending text, so its scroll to the bottom on each append. I want to implement follow-tail option (while appending text scroll over the text box) on that multi-line text. Any comments please...

I try to get the number of line up to the bottom edge of the text box and set the scroll to that, but I couldn't do that.

Thanks in advance :)
Posted
Comments
Sergey Alexandrovich Kryukov 19-Nov-13 11:57am    
To start with: if you say "TextBox", the question is: which one? Full type name, please.
Generally, TextBox types are not well suited for appending. Each time you have to read and assign the whole Text. What's the purpose of it? If you use it for a kind of a console, you should better use some other Control or UIElement...

—SA
BillWoodruff 19-Nov-13 12:48pm    
Sergey, in WinForms both TextBox and RichTextBox controls provide the AppendText() method (defined in TextBoxBase, from which both inherit), which, I assume, is optimized at a low-level.

That said, in my own practice, I am always trying to find a way to use a StringBuilder which is converted to a String only when necessary.
Sergey Alexandrovich Kryukov 19-Nov-13 13:30pm    
Oops! My bad...
Thank you for pointing it out.
—SA
BillWoodruff 19-Nov-13 12:42pm    
Please explain exactly what you mean by "follow-tail."
CodingLover 20-Nov-13 8:43am    
Its like, in a text box you are keep adding content and it scrolling down to display the latest content. In case I want to see the previous content I have to scroll the vertical scroll bar to the top, but I can't because the content adding scroll down. I just want control that.

1 solution

By follow-tail I take it to mean that you want to continuously append text to the end of the textbox and have the textbox automatically scroll so that the new text is visible.

You've already set the text box to multiline, and I advise that you add vertical scrollbar if you haven't already.

The following code will move you to the bottom of the text box each time you add a line

VB
TextBox1.Text += TweetText + System.Environment.NewLine
textBox1.SelectionStart = textBox1.Text.Length
textBox1.ScrollToCaret()


EDIT - I'll try that again in C# !!!
C#
textBox1.Text += (TweetText) + System.Environment.NewLine;
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();


Where TweetText is a string
 
Share this answer
 
v2
Comments
CodingLover 20-Nov-13 8:48am    
Thanks for you comment :)

As you said the text box is a multiline text box and I have added vertical scroll bar too. As you know each time I append the text its automatically scroll to the bottom of the text box. I tried the code above, but once I scroll the scroll bar up/down it didn't work. Rather appending at the bottom its added here and there.
CHill60 20-Nov-13 9:00am    
Might be worth doing the selectionstart and scrolltocaret before adding the next text rather than the order I put it in above
CodingLover 20-Nov-13 9:13am    
I tried that. But then the scroll bar always stuck at top and keep adding the text at the end...
CHill60 20-Nov-13 9:29am    
Can you post the line of code you are using to add the new line of text to the text of the textbox?
CodingLover 21-Nov-13 2:49am    
Yeah, sure.

if (chkFollowTail.Checked)
{
txtEventResults.AppendText(sbEventRes.ToString());
}
else
{
txtEventResults.SelectionStart = txtEventResults.Text.Length;
txtEventResults.ScrollToCaret();
txtEventResults.Text += sbEventRes.ToString();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900