private void ColorRichTextbox(RichTextBox box, string text, Color color, bool appendNewLine = true)
{
int length = box.TextLength;
if (!string.IsNullOrWhiteSpace(box.Text) && appendNewLine)
{
box.AppendText("\r\n" + text);
}
else
{
box.AppendText(text);
}
box.SelectionStart = length;
box.SelectionLength = text.Length;
box.SelectionColor = color;
}
Then in button click event the first line in the richTextBox will be light green :
ColorRichTextbox(richTextBoxLogChanges, "Watching Event Started : ", Color.White);
ColorRichTextbox(richTextBoxLogChanges, DateTime.Now.ToString(), Color.LightGreen, false);
Then i'm adding another line in yellow :
private void Fsw_Deleted(object sender, FileSystemEventArgs e)
{
ColorRichTextbox(richTextBoxLogChanges, $"Deleted: {e.FullPath}", Color.Yellow);
}
The yellow line the last char get the light green color of the first line :
In the screenshot in this example the last char S of the css is in light green and it should be in yellow.
Screenshot of the last problem mentioned in my comment/s to the solution :