Reputation: 599
I want to keep a certain number of lines in the richtextbox, so I did the following:
private void txt_Log_TextChanged(object sender, EventArgs e)
{
int max_lines = 200;
if (txt_Log.Lines.Length > max_lines)
{
string[] newLines = new string[max_lines];
Array.Copy(txt_Log.Lines, 1, newLines, 0, max_lines);
txt_Log.Lines = newLines;
}
txt_Log.SelectionStart = txt_Log.Text.Length;
txt_Log.ScrollToCaret();
}
But when running my Richtextnbox blinks continuously, so how should I make this smooth ?
Upvotes: 0
Views: 360
Reputation: 1181
One option would be to unhook the listener, perform the update, then re-hook the listener upon exiting the function.
private void txt_Log_TextChanged(object sender, EventArgs e)
{
txt_Log.TextChanged -= this.txt_Log_TextChanged;
//make you're updates here...
txt_Log.TextChanged += this.txt_Log_TextChanged;
}
Upvotes: 1
Reputation: 14059
When you update a value of the txt_Log.Lines
property, the txt_Log_TextChanged
event fires again.
You could use a bool
variable to stop it:
private bool updatingTheText;
private void txt_Log_TextChanged(object sender, EventArgs e)
{
if (updatingTheText)
return;
const int max_lines = 200;
if (txt_Log.Lines.Length > max_lines)
{
string[] newLines = new string[max_lines];
Array.Copy(txt_Log.Lines, 1, newLines, 0, max_lines);
updatingTheText = true; // Disable TextChanged event handler
txt_Log.Lines = newLines;
updatingTheText = false; // Enable TextChanged event handler
}
txt_Log.SelectionStart = txt_Log.Text.Length;
txt_Log.ScrollToCaret();
}
Upvotes: 0