Results 1 to 11 of 11

Thread: Adding Colored Text To RichTextBox

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Adding Colored Text To RichTextBox

    Hi guys,

    please help me with the thing I'm stuck.
    I'm stuck because I don't know how to add colored text to a richtextbox.

    To make already existing text colored, I have tried things like:

    Code:
    RichTextBox1.SelectionStart = RichTextBox1.Find("colored word")
    RichTextBox1.SelectionColor = Color.Blue
    RichTextBox1.SelectionLength = 0
    ..so it will change the color of the found word.

    The problem is, when the "colored word" exists multiple times in the textbox, it will never change theirs color. It only searches for the first "colored word" and it wont affect the others.

    It would be even better when someone knows how to directly add colored text instead of changing it when it's added.

    Any help available?
    Last edited by Cooi; Jan 25th, 2010 at 12:51 PM.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Adding Colored Text To RichTextBox

    I'm afraid that you only control the color of the text with the selection object.
    You can iterate through each occurrence and change the color of the text unless you want to control your text directly with RTF tags.

  3. #3
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Re: Adding Colored Text To RichTextBox

    To add coloured text as you are typing use:

    Code:
    Private Sub btnColours_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColours.Click
    
        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextEditor.SelectionColor = ColorDialog1.Color
        End If
    
    End Sub
    Computerman
    It was much easier in VB6, but I am now liking Vb.Net alot more.

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Re: Adding Colored Text To RichTextBox

    You misunderstood me.

    Without any color dialog, without any color source. Just add colored text.
    I know perfectly how to allow people to change the color in text,
    but it's not my question.

    It's how to do automatically, not manually.

  5. #5

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Re: Adding Colored Text To RichTextBox

    I know I should use SelectionColor,
    but my question is:
    How do I find the wanted words and select them ALL? (not at once)

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Adding Colored Text To RichTextBox

    Quote Originally Posted by Cooi View Post
    I know I should use SelectionColor,
    but my question is:
    How do I find the wanted words and select them ALL? (not at once)
    With the existing control you can't do that. Implement your own search then. Gather the contents of your richtextbox to a string buffer and perform the search through the string variable. Remember the positions of the matches and apply your colors one by one. I'm afraid there's no other option unless you want to develop your implementation of a rich text box.

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Re: Adding Colored Text To RichTextBox

    You're right, that's what I want.

    Now, the only problem is...

    I don't know how to search for all words I want to be colored.

    The RichTextBox1.Find("the colored string") doesn't work.

    It only searches for the first "the colored string" and doesn't affect the others.

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Adding Colored Text To RichTextBox

    The RichTextBox's Find method has an overload where you specify the starting index to find the word. You have to increase the start index by the previous found word's index so that it can find the next word. The Find method also tells you the index at which it found the found, so you increase the start index by the return value of the Find function.

  10. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Adding Colored Text To RichTextBox

    Use this overload declaration of the Find method:

    Find( str As String, Start As Integer, options As System.Windows.Forms.RichTextBoxFinds)

    You need the Start parameter to specify where to start your search again.

    vb.net Code:
    1. Dim Pos As Integer = rtf.Find("mymatch", 0, RichTextBoxFinds.WholeWord)
    2. ' You may use different find options instead of WholeWord
    3.  
    4. Pos+=1
    5.  
    6. Dim NewPos As Integer = rtf.Find("mymatch", Pos, RichTextBoxFinds.WholeWord)
    7. ' etc
    8. ' got it?

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Re: Adding Colored Text To RichTextBox

    Thanks, I found it.
    I didn't see this overload yet, because I thought all overloads after the string were for RichTextBoxFinds options.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width