Thread: Search for words and change their color

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Search for words and change their color

    If you have a richTextBox that contains these words.
    What is the best/good way to do, if I want to change the color of the word "hello" to Ex: red.
    Is there any searchfunction that can select multiple words etc...
    I am trying to find a way to do this.


    one two three hello
    five hello
    hello

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    I have a complete mind block on the actual code, but know the process... (!)

    Put all the richTextBox contents in to a string, replace search term in string with the word itself and the formatting needed.

    ie. (but not actually this! This example replaces the term wanted with a bolded word in html format)

    Code:
    public static void ReplaceText(string myTerm)
    {
      string myString = richTextBox1.Text;
      myString.Replace(myTerm, "<b>"+myTerm+"<\b>");
      richTextBox1.Text = myString;
    }
    Then you can just feed in whatever term you want ("hello") like so:

    Code:
    ReplaceText("hello");
    I'm bored, needed to do something random...
    Last edited by DanFraser; 03-26-2008 at 07:24 AM.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. adding a function to a program
    By trippedwire in forum C++ Programming
    Replies: 9
    Last Post: 09-30-2004, 12:35 PM
  3. Trying to find a way to change color
    By Vanished in forum C++ Programming
    Replies: 2
    Last Post: 11-24-2002, 11:16 AM
  4. Replies: 2
    Last Post: 09-04-2001, 02:12 PM
  5. is there a way to set a pixel or change font color?
    By Flikm in forum C++ Programming
    Replies: 9
    Last Post: 09-01-2001, 01:24 PM