WinFormsアプリケーションには、ログファイルとして使用しているTextBoxがあります。を使用してフォームのちらつきなしでテキストを追加していますが、TextBox.AppendText(string);
古いテキストを削除しようとすると(コントロールの.Textプロパティが.MaxLength制限に達すると)、ひどいちらつきが発生します。
私が使用しているコードは次のとおりです。
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();
}
}
ちらつきを引き起こさない、コントロールの上部からテキストの行を削除するすてきな方法はありますか?テキストボックスには、ListViewにあるBeginUpdate()/ EndUpdate()メソッドがありません。
TextBoxコントロールは、コンソールログに最適なコントロールでさえありますか?
編集:TextBoxのちらつきは、テキストボックスが上にスクロールしているように見え(コントロールの上部にあるテキストを削除している間)、すぐに下にスクロールして戻ります。-すべてが非常に迅速に行われるため、ちらつきが繰り返されます。
私もこの質問を見たばかりで、リストボックスを使用することを提案しましたが、(ほとんどの場合)リストボックスのテキストを1文字で受信しているため、これが私の状況で機能するかどうかはわかりません。時間。