How do i make a textbox that uses different forecolors?

I would like to make a textbox that uses a color for a word and uses another color for other words.

is there any way to do this



Answer this question

How do i make a textbox that uses different forecolors?

  • beezorz

    Yes, you can do this pretty easily with the RichTextBox control. You can find a list of documentation at: http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcon/html/vboririchtextboxctltasks.asp

     

    Hope this helps,

    Dan Fernandez
    Product Manager
    Visual Studio Express
    http://blogs.msdn.com/danielfe/


  • GAKannan

    Here's something that will work, it's not perfect as it doesn't differentiate between portions of words (E.g. World and Worldwide) but it may drive you closer to your goal.

     

    private void HighLightWord( string Word, Color TheColor ) {

    // Locals

    int lastfound = 0;

    // Flip case

    Word = Word.ToUpper();

    if (this.richTextBox1.Text.ToUpper().Contains(Word))

    {

    while (richTextBox1.Text.ToUpper().IndexOf(Word, lastfound) != -1)

    {

    // Find text (you must ignore case since IndexOf is case sensitive

    int Index = richTextBox1.Text.ToUpper().IndexOf(Word, lastfound);

    // Set to new color

    richTextBox1.Select(Index, Word.Length);

    richTextBox1.SelectionColor = TheColor;

    // Increment

    lastfound = Index + 1;

    }

    }

    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)

    {

    // Save the current selection

    int currentStart = richTextBox1.SelectionStart;

    int currentLength = richTextBox1.SelectionLength;

    // Change all to black

    richTextBox1.Select(0, richTextBox1.TextLength);

    richTextBox1.SelectionColor = Color.Black;

    // Now colorise

    HighLightWord("Test", Color.Red);

    HighLightWord("Hello", Color.Blue);

    HighLightWord("World", Color.Green);

    // Restore current seleciton

    richTextBox1.Select(currentStart, currentLength);

     

    }


  • Misu_for_friends

    oh, thanks

    I been looking for a way of doing that for a long time.

    now i know!


  • Kapop

    mmm...

    i tried using the select and selectcolor methods,

    but it isnt worhking.

    i had something like this:

     

    public void HighLight(string Word, Color color)

    {

    if (this.richTextBox1.Text.Contains(Word)

    {

    int Index = richTextBox1.Text.IndexOf(Word, 0, 0);

    richTextBox.Select(Index, Word.Lenght);

    richTextBox.SelectionColor = color;

    }

    }


  • bigbrains

    Oh thanks, it works

    the only problem is that the text flickers

    is there a way to prevent this from happening


  • How do i make a textbox that uses different forecolors?