Bite my bytes

What I learn by day I blog at night - A blog from Microsoft Consultant working from Ljubljana, Slovenia

  Home :: Contact :: Syndication  
  999 Posts :: 7691 Comments :: 235 Trackbacks

Search

Most popular posts
in last 30 days

Categories

My Projects

Archives

Stuff


Copyright © by David Vidmar
 
Contact me!
 
LinkedIn Profile
 
 
 

Let's say, you want to create a multi-line TextBox and put some text in it. And you want the TextBox to automatically scroll as you add the text. Nothing easier, just += new text to the Text property. You add the text, but you don't scroll it so the next hing you would probably try would be using SelectionStart() and ScrollToCaret() methods. It would work but also it would flicker a lot.

The right way is to assign new text to SelectedText property. This way you add text, scroll down and do that without flicker. Who would have thought? Not very intuitive, right?

textBox.Text = "First line.";
textBox.SelectionStart = textBox.TextLength;
for (int i = 0; i < 100; i++)
{
  textBox.SelectedText = "Another line! #" + i.ToString() + Environment.NewLine;
}

All said goes for .NET 1.1. I checked 2.0 beta2 and it doesn't seem to have any better way for deailing with this problem.

[Update] Andrej pointed out, that there IS a better way to do this. The method is called AppendText() and it does just what I didn't find - appends the text and scrolls the scrollbar if necessary! Thanks, Andrej!

Internaly, AppendText() method does what I described above. "Luckily" there is an issue with text longer than 32767 bytes, so maybe I will not look like a complete jerk for posting this. To get arround the issue you can use my solution above or override the AppendText() method.

Posted on Monday, August 15, 2005 7:44 PM | Filed under: Developement |

Feedback

# re: TextBox as a log 8/15/2005 10:12 PM Andrej
Hi David,
nice trick, but couldn't you just use .Append() method?

textBox.AppendText("Another line! #" + i.ToString() + Environment.NewLine);

# re: TextBox as a log 8/15/2005 10:16 PM Andrej
That's .AppendText() method :-*

# re: TextBox as a log 8/15/2005 10:29 PM David
Tnx, Andrej! I have no idea how I was able to miss that one. I looked quite hard. I guess just not hard enough. ;)

# re: TextBox as a log 8/15/2005 10:58 PM Andrej
... and I'm happy to report VS2005 doesn't seem to have any problems regarding 32K limit anymore (I've just successfully tested it up to 1 Meg of text) ;)

# re: TextBox as a log 8/15/2005 11:05 PM David
I just wanted to test that! Thanks for your help. :)

# Multiline TextBox Control Flickering Problem in C# -- Help 2/13/2008 11:55 AM Bishnu Gopal Patro
Hi,

In my C# application there is a multiline textbox with vertical scrollbar. When the multiline textbox get updated with new data, the vertical scrollbar flickers (from top to bottom) with each update on the textbox. How can I prevent the scrollbar flickering when the multiline textbox get updated each time.

Thanking you in advance.

Bishnu

# re: TextBox as a log 2/15/2008 9:53 AM David
Try using AppendLine(), that should solve your problem.

# re: TextBox as a log 2/18/2008 1:16 PM Bishnu Gopal Patro
I tried that.. It is not working....
flickering problem still persists...

# re: TextBox as a log 2/19/2008 5:51 AM Bishnu Gopal Patro
The textbox contains some intial text. Before we append some new text to the existing textbox, first we create a temporary stringarray variable which will contains the existing text and new text. After that we are setting this string array variable with the text.lines property.


ex: base.Lines = newLines;

where base is RichTextBox and Lines is the property of richtextbox. And newlines is of type string[].

# re: TextBox as a log 5/5/2010 1:38 PM Vitesh
Just what I'm looking for. Simple, effective and works very well.

Thanks guys !

Comments have been closed on this topic.