Solved

.NET Windows Form RichTextBox append text causes color inconsistency

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now