Note that there are some explanatory texts on larger screens.

plurals
  1. POStopping TextBox flicker during update
    text
    copied!<p>My WinForms application has a TextBox that I'm using as a log file. I'm appending text without the form flickering using <code>TextBox.AppendText(string);</code>, however when I try to purge old text (as the control's .Text property reaches the .MaxLength limit), I get awful flicker.</p> <p>The code I'm using is as follows:</p> <pre><code>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 &gt; textBox.MaxLength) { int cr = textBox.Text.IndexOf("\r\n"); if (cr &gt; 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(); } } </code></pre> <p>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.</p> <p>Is a TextBox control even the best suited control for a console log?</p> <p>Edit: The TextBox flickering appears to be the textbox scrolling up to the top (while I purge the text at the top of the control), and then it immediately scrolls back down to the bottom. - it all happens very quickly, so I just see repeated flickering.</p> <p>I've also just seen <a href="https://stackoverflow.com/questions/383813/best-approach-to-windows-forms-rolling-log-output-in-textbox">this question</a>, 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload