Solved

.NET Windows Form RichTextBox append text causes color inconsistency

Posted on 2009-05-08
1
1,013 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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