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 |