Solved

.NET Windows Form RichTextBox append text causes color inconsistency

Posted on 2009-05-08
1
1,027 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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