4

I currently have a function that will set a value to a RichTextBox, although how could you "add" a value or a new line to it, rather than overwriting existing data in the RichTextBox?

richTextBox2.Text = DateTime.Today + " Hello";
1
  • James, I recommend you to use a single OpenId to log in, so you will be the same user as usually Commented Jun 26, 2011 at 16:50

5 Answers 5

19
richTextBox2.AppendText(Environment.NewLine + DateTime.Today + " Hello"); 
Sign up to request clarification or add additional context in comments.

Comments

10
richTextBox2.AppendText(String.Format("{0} the date is {1}{2}", "Hello", DateTime.Today, Environment.NewLine));

Please don't use +

2 Comments

Why no "+"? Just for readability? Or is there an other reason?
@JosuaSchmid strings are immutable. Using + to chain n of them (especially for big strings and/or many catenations) creates n-1 strings in memory one bigger than the last one, resulting in a (temporary) waste of memory and a lot of computing overhead. This is why using String.Format() and the StringBuilder-Class is prefered in favor of +
4
richTextBox2.Document.Blocks.Clear();
richTextBox2.Document.Blocks.Add(new Paragraph(new Run("string")));

1 Comment

The user asked for winforms not for WPF.
0

I wrote this as a simple log-to-screen kinda thing to inform user of stuff going on in the background or to acknowledge various actions.

Add a RichTextBox on the form, here I call it "rtbLog"

Then call the below and add your message and any optional formatting you want. So for example:

DoWriteToLog("Hey, welcome!", true, Color.Green)


public void DoWriteToLog(string strLogMessage, bool blnHighlighted = false, Color myFontColor = default(Color), bool blnNewLine = true)
        {
            string dtStamp = "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
            if (blnNewLine)
                strLogMessage = dtStamp + strLogMessage + Environment.NewLine;
            if (blnHighlighted)
            {                
                rtbLog.SelectionColor = myFontColor;             // Optional font color passed to method
                if (strLogMessage.ToLower().Contains("help"))       // IF there is 'help' in the message, highlight it blue
                    rtbLog.SelectionBackColor = Color.LightBlue;
                rtbLog.SelectionFont = new Font("Cambria", 10F, FontStyle.Bold, GraphicsUnit.Point);
                rtbLog.AppendText(strLogMessage);                
            }
            else            
                rtbLog.AppendText(strLogMessage);
            rtbLog.ScrollToCaret();
        }

The above DoWriteToLog statement will write "Hey, welcome!" to the RichTextBox in bold Green font. It adds a timestamp to each line entered as well, but you can avoid that by turning off blnNewLine. Of course it's specific to my use, but you can modify it to suit yours.

I also have a help 'detector' in there so if I am putting a help message on the screen it highlights it a specific color, you can comment/delete those two lines as needed.

Note the only required parameter, of course, is the message. This way, since I use this a lot, I don't have to add all the parameters every time thus, saving lots of typing :-)

Comments

-1
richTextBox2.Text = "Something " + richTextBox2.Text + " and something"

6 Comments

@abatishchev There is more than one function adding text to this, hence I don't think your solution would work
This is going to be horribly inefficient when there is a lot of text in the control
@David: Nothing said in the answer about a lot of text
reading the entire contents of the control just to add another line of text is very inefficient. Why go out of your way to write inefficient code?
@David: Does RichTextBox.Text property read entire control content rather than holds it into a variable? I didn't knew that.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.