![]() |
Languages »
C# »
Windows Forms
Intermediate
License: The Code Project Open License (CPOL)
Multiple Colored Texts in RichTextBox using C#By S.VinothkumarMultiple Colored texts in RichTextBox using C# |
C# (C#2.0), .NET (.NET2.0), Visual-Studio (VS2005), WinForms, Architect, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article is created for making multiple colored texts in a textbox. By using the SelectionText property in richtextbox we have created a sample application for multiple colored texts in Microsoft’s richtextbox control. Let us see how to make it.
Actually one of my colleagues asked me how to enter values in a richtextbox with multiple color texts.
I just tried to find any other way to have multiple colors in a textbox, unfortunately did not find one. Then I tried in Google and found that we have an option to do it in richtextbox. There is a property to set colored text by using SelectionText.
Richtextbox has the properties of SelectionFont, SelectionColor and SelectedText. Setting the values for these properties makes multiple colors in our richtextbox.
I just tried to create it as a sample application and it is done. It has a very simple logic to it. I have given the code snippet with this article. You can download the attached sample application and try to see the magic by clicking the buttons. There are two buttons available to test. One is for red colored text and the other is for green colored text. You can change the colors as per your wish by changing the codebehind.
The following code snippet explains to us how to do the multiple colored texts in textbox. Let us see:
Font font = new Font("Tahoma", 8, FontStyle.Regular);
richTextBox1.SelectionFont = font;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectedText = Environment.NewLine + textBox1.Text;
Using the above code, I have created a sample application and attached it with this article (please see download link at the top of this article).
Hence we have made multiple colored texts in our richtextbox. This is basically used for making some chat application. We will have to show the text of the person at the other end in a different color in the chat application, so that we can use this method. We can create a simple method and call in every time we need to show the text in richtextbox with parameters like “textbox1.text, color, etc.”
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 Jun 2009 Editor: Deeksha Shenoy |
Copyright 2009 by S.Vinothkumar Everything else Copyright © CodeProject, 1999-2010 Web20 | Advertise on the Code Project |