Multiple Colored texts in RichTextBox using C#

No.of Views1783
Bookmarked0 times
Downloads 
Votes0
By  vinothnat   On  15 Feb 2010 22:02:38
Tag : CSharp , Windows Forms
Multiple Colored texts in RichTextBox using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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 in below.

Prerequisite

1. .NET Framework 2.0

2. Visual Studio 2005


Description

actually one of my colleague asked me that how to enter values in richtextbox with multiple color texts.

I just tried to find any other way to have multiple colors in a textbox, unfortunately no. Then I tried in google and found that we have an option to do in richtextbox. There is a property to set colored text by using SelectionText.

Richtext box having the properties of SelectionFont, SelectionColor and SelectedText. Setting the values for these properties makes multiple colors in our richtext box.

I just tried to make it as a sample application and done. It's a very simple logic to do. 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 is two buttons available to test. One is for red colored text and one is for green colored text. You can change the colors as your wish by changing the codebehind.


Implementation

the following code snippet is explained us how to do the multiple colored texts in textbox. Let us see,

[code]

Font font = new Font("Tahoma", 8, FontStyle.Regular);

richTextBox1.SelectionFont = font;

richTextBox1.SelectionColor = Color.Red;

richTextBox1.SelectedText = Environment.NewLine + textBox1.Text;

[/code]

Using the above code, I have created a sample application and attached. Find the attachment.

Conclusion

Hence we have been made a multiple colored texts in our richtextbox. This is basically used for doing some chat application. We will have to show the other end of person's text in different color in chat application. So that we can use this method. And we can create a simple method and calling in every time of showing the text in richtextbox with the parameters of like "textbox1.text, color,..etc".

That's it...

...S.VinothkumaR.

http://vinothnat.blogspot.com

 
Sign Up to vote for this article
 
About Author
 
vinothnat
Occupation-Not Provided
Company-Not Provided
Member Type-Junior
Location-Not Provided
Joined date-03 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top