views:

907

answers:

3

My WinForms application has a TextBox that I'm using as a log file. I'm appending text without the form flickering using TextBox.AppendText(string);, however when I try to purge old text (as the control's .Text property reaches the .MaxLength limit), I get awful flicker.

The code I'm using is as follows:

public static void AddTextToConsoleThreadSafe(TextBox textBox, string text)
{
    if (textBox.InvokeRequired)
    {
        textBox.Invoke(new AddTextToConsoleThreadSafeDelegate(AddTextToConsoleThreadSafe), new object[] { textBox, text });
    }
    else
    {
        // Ensure that text is purged from the top of the textbox
        // if the amount of text in the box is approaching the
        // MaxLength property of the control

        if (textBox.Text.Length + text.Length > textBox.MaxLength)
        {
            int cr = textBox.Text.IndexOf("\r\n");
            if (cr > 0)
            {
                textBox.Select(0, cr + 1);
                textBox.SelectedText = string.Empty;
            }
            else
            {
                textBox.Select(0, text.Length);
            }
        }


        // Append the new text, move the caret to the end of the
        // text, and ensure the textbox is scrolled to the bottom

        textBox.AppendText(text);
        textBox.SelectionStart = textBox.Text.Length;
        textBox.ScrollToCaret();
    }
}

Is there a neater way of purging lines of text from the top of the control that doesn't cause flickering? A textbox doesn't have the BeginUpdate()/EndUpdate() methods that a ListView has.

Is a TextBox control even the best suited control for a console log?

Edit: The TextBox flickering appears to be the textbox scrolling up to the top (while I purge the text at the top of the controol), and then it immediately scrolls back down to the bottom. - it all happens very quickly, so I just see repeated flickering.

I've also just seen this question, and the suggestion was to use a ListBox, however I don't know if this will work in my situation, as (in most cases) I'm receiving the text for the ListBox one character at a time.

+1  A: 

Have you set double-buffering on your main window?

this code in your constructor after the InitializeComponent call will add double buffering and possibly reduce flicker.

this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer,true);

Alastair Pitts
I've tried this, unfortunately it doesn't make any difference.
Bryan
This won't help; TextBox is inherently flickery and the best you can hope is reduce it. http://stackoverflow.com/questions/1333393/how-to-prevent-a-windows-forms-textbox-from-flickering-on-resize
romkyns
+1  A: 

Did you try SuspendLayout() / ResumeLayout() around all your update operations?

You could also call Clear() on the textbox then reassign the truncated text.

If your try to implement some kind of log file viewer, you could use a ListBox instead.

codymanix
I tried both double buffering and SuspendLayout() / ResumeLayout(), unfortunately they don't seem to make any difference.I've updated my question about using a ListBox, I'm not sure it will work due to the fact that I (usually) add one character at a time.
Bryan
you can update the last item in the listbox to add a character and if the line is full you can append a new ListBox item.
codymanix
Did you include *all* of your operations in SuspendLayout()/ResumeLayout(), include the ScrollToCaret call?
codymanix
Yes, I put the suspend/resume as the first and last statement in the invoked part of the method.
Bryan
Also, thinking about it some more, I don't think a listbox will work, as the users need to be able to copy text to the clipboard. Theyneed to be able to select the text they want to copy using click/drag.
Bryan
I've also tried passing the form object to the static method and using Suspend/resume on that too.
Bryan
You can implement copy functionality for a ListBox with the ClipBoard class, if that helps.
codymanix
+1  A: 

The problem is that you are adding (removing) one character at a time repeatedly and quickly. One solution would be to buffer the characters as they are being added and update the textbox at greater intervals (regardless of the amount of characters), for example, every 250 milliseconds.

This would require:

  • to have an array or stack of characters where they get added
  • to have a timer that would call a delegate that would actually do the update with the characters stored in the stack

Another option is to use both every 250 ms and 100 chars, whatever happens first. But this would probably complicate the code more without any tangible benefit.

Vinko Vrsalovic
Would that not just increase the rate of flickering?
Bryan
That's given me an idea though which I can use until I find a better solution. When the control fills, I now purge around 20% of the content (I can afford to drop 20%, although I'd rather have it work properly). This way instead of seeing the flickering constantly when the control is full, you are unlikely to notice it as the flicker might only occur once an hour instead of several times a second.
Bryan
The periods were without much a thought, I must admit :)
Vinko Vrsalovic
Despite me accepting this question, the best answer I found was Vinko's comment in reply to my original question.
Bryan