How to Highlight Text Within a RichTextBox Using C# .NET WIndows Forms

Posted on 2011-10-26
Last Modified: 2013-12-16
I am attempting to write a RichTextBox and I need to be able to write part of a sentence highlighted. or change the color of the text of a specific part of a string, and the remainder of the text in the sentence in regular type. Could someone help me figure out what the syntax would be in order to complete this task? For instance, if the control was called RichTextBox1 and the sentence was "The dog crossed the road", how would I get the word "dog" to be highlighted, or in a different font color, and the remainder of the sentence to be regular type?

Any help that could be provided on this topic would be greatly appreciated.
Question by:thenthorn1010
    LVL 74

    Accepted Solution

    Something like this should do it:

    string find = "dog";
    int start = RichTextBox1.Text.IndexOf(find);
    RicthTextBox1.SelectionStart = start;
    RicthTextBox1.SelectionLength = find.Length;
    RicthTextBox1.SelectionColor = Color.Red;

    Open in new window

    LVL 18

    Assisted Solution

    by:Anil Golamari

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now