1

The class at the top of form1 :

public static class RichTextBoxExtensions
    {
        public static void AppendText(this RichTextBox box, string text, Color color)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;

            if (!string.IsNullOrWhiteSpace(box.Text))
            {
                box.AppendText("\r\n" + text);
            }
            else
            {
                box.AppendText(text);
            }

            box.SelectionColor = box.ForeColor;
        }
    }

and how i'm using it :

this.Invoke(new Action(
delegate ()
{
    RichTextBoxExtensions.AppendText(richTextBox1, $"Created: {e.FullPath}", Color.GreenYellow);
    RichTextBoxExtensions.AppendText(richTextBox1, $" On: {DateTime.Now}", Color.Yellow);
}));

The problem is that it's adding two lines to the richTextBox one in GreenYellow and then a new line in Yellow.

but i want this to be in once line : GreenYellow and Yellow and then after that if adding another text that the new text to be in a new line.

This is a screenshot of how it is now :

as it is now

and this screenshot is ho i want it to be both on same line and after that if a new text will be added the new text will be in a new line :

as i want it to be

Yosi Ashbel
  • 123
  • 7
  • 1
    You might find this link useful: https://stackoverflow.com/questions/73270757/color-change-of-selected-text-not-working/73272683#73272683 – R_J Aug 09 '22 at 20:30
  • 1
    Well, you have this: `box.AppendText("\r\n" + text);` in your code. Why did you add it if you don't want the text to go on a new line? Just remove it. Pass a string that starts with `\n` if you want to generate a new line (the RichTexBox uses only `\n`, not `\r\n`, all `\r` are parsed and discarded) -- BTW, in that method you just need `box.SelectionColor = color; box.AppendText(text); box.SelectionColor = box.ForeColor;`. Pass `Color.Empty` to use the default Color. – Jimi Aug 09 '22 at 20:41

1 Answers1

1

I think you need to reverse your logic from:

        if (!string.IsNullOrWhiteSpace(box.Text))
        {
            box.AppendText("\r\n" + text);
        }
        else
        {
            box.AppendText(text);
        }

To:

        if (!string.IsNullOrWhiteSpace(box.Text))
        {
            box.AppendText(text);
        }
        else
        {
            box.AppendText("\r\n" + text);
        }
OldDog
  • 334
  • 4
  • 12