Click here to Skip to main content
Licence CPOL
First Posted 26 Jun 2009
Views 30,152
Downloads 1,009
Bookmarked 17 times

Multiple Colored Texts in RichTextBox using C#

By S.Vinothkumar | 26 Jun 2009
Multiple Colored texts in RichTextBox using C#
8 votes, 61.5%
1

2

3
3 votes, 23.1%
4
2 votes, 15.4%
5
2.31/5 - 13 votes
μ 2.13, σa 3.12 [?]
multiplecolor1.JPG

multiplecolor2.JPG

Introduction

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.

Prerequisites

  1. .NET Framework 2.0
  2. Visual Studio 2005

Description

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.

Using the Code

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).

Conclusion

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.”

History

  • 27th June, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

S.Vinothkumar

Web Developer

India India

Member
Hi Viewers,

I wish to all. This is Vinoth. This is where I try to condense everything that you need to know about me.
 
Blog:
 
visit my blog
 
Interests:

I'm passionate about a great many things and continually learning about the things that interest me. They are wearable computers, User Interface Design, Artificial life, Industrial music.
 

 

 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberSercanOzdemir4:42 10 Nov '11  
GeneralSimple but helpful information PinmemberEdo Gasali23:52 7 Apr '11  
GeneralMy vote of 1 Pinmembergurisoft6:35 4 Mar '11  
GeneralIf selecting in box Pinmemberginbot10:26 27 Feb '11  
GeneralMy vote of 1 Pinmemberginbot10:21 27 Feb '11  
GeneralMulti Colors to Multi Texts in a Single Line PinmemberMember 43190280:18 20 Jan '10  
GeneralRe: Multi Colors to Multi Texts in a Single Line PinmemberAnt210013:02 17 Jun '10  
GeneralRe: Multi Colors to Multi Texts in a Single Line PinmemberGPUToaster20:44 19 Jun '10  
GeneralRe: Multi Colors to Multi Texts in a Single Line PinmemberAnt210021:51 19 Jun '10  
GeneralMy vote of 1 PinmemberPhil J Pearson0:55 23 Dec '09  
GeneralMy vote of 1 PinmemberVMykyt21:56 16 Jul '09  
GeneralMy vote of 1 PinmemberVMykyt21:41 16 Jul '09  
GeneralMy vote of 1 Pinmemberhoasidpfuaokjsñeirf3:36 2 Jul '09  
GeneralMy vote of 1 PinmemberJon Artus5:54 1 Jul '09  
GeneralMy vote of 1 PinmemberMember 604014514:39 27 Jun '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120209.1 | Last Updated 27 Jun 2009
Article Copyright 2009 by S.Vinothkumar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid