0

I have a richTextBox control on my Windows Forms C# Application.

In order to be able to dynamically add text with different colors, I created a separate class:

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;
       box.AppendText(text);
       box.SelectionColor = box.ForeColor;
   }

}

I tried to implemented the integration by doing the following in my Load function (logBox is the name of my richTextBox control):

logBox = new RichTextBox { Dock = DockStyle.Fill, Font = new Font("Courier New", 10) };
logBox.AppendText("[" + DateTime.Now.ToShortTimeString() + "]", Color.Blue);
logBox.AppendText("Preparing to fetch Instructions...");

This does not seem to work.

Any suggestions?

2
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception, post the line it occurred on and the exception details.
    – gunr2171
    Nov 20, 2014 at 18:14
  • Make sure you've imported the namespace your extension method class lives in into your form's code-behind class.
    – Cᴏʀʏ
    Nov 20, 2014 at 18:16

1 Answer 1

0

I apologize, I simply got rid of:

logBox = new RichTextBox { Dock = DockStyle.Fill, Font = new Font("Courier New", 10) }; 

and now it works. I was expecting my existing richTextBox, logBox, to populate with text but this was not occurring until now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.