Solved

.NET Windows Form RichTextBox append text causes color inconsistency

Posted on 2009-05-08
1
1,012 Views
Last Modified: 2013-12-17
This is a very unique problem I am facing.

In .NET 2.0, if you want to append formatted text (with font styles/color) into a rich textbox, you use this code :


richtextbox.SelectionFont = fontFromFontDialog;
richtextbox.SelectionColor = colorFromColorDialog;
richtextbox.AppendText("Some text");


the font and color are obtained from a color and font dialog.

For the first few times, the colors are correct. however if i keep changing font and colors, the colors get mixed up. If i select orange, it might appear blue on the richtextbox.

Anyone can give a solution? Really surprusing..


richtextbox.SelectionFont = fontFromFontDialog;
richtextbox.SelectionColor = colorFromColorDialog;
richtextbox.AppendText("Some text");
 
// keep looping this with different colors/fonts will give wrong colors

Open in new window

0
Comment
Question by:transcend3nt
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 24336361
Before setting SelectionFont and SelectionColor you need to make sure that the cursor is at the END of your RichTextBox (it could be moved by the user or possibly thru other code):



            richtextbox.SelectionStart = richtextbox.TextLength; // move cursor to the END first!
            richtextbox.SelectionFont = fontFromFontDialog;
            richtextbox.SelectionColor = colorFromColorDialog;
            richtextbox.AppendText("Some text");
            // (just FYI)...an alternative to AppendText():
            richtextbox.SelectedText = "Some text";

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question