|
-
Jan 25th, 2010, 11:50 AM
#1
Thread Starter
Member
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.
-
Jan 25th, 2010, 01:21 PM
#2
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.
-
Jan 25th, 2010, 04:09 PM
#3
Hyperactive Member
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. 
-
Jan 26th, 2010, 11:23 AM
#4
Thread Starter
Member
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.
-
Jan 26th, 2010, 11:43 AM
#5
Re: Adding Colored Text To RichTextBox
You picked the wrong container. You should probably stick to html instead.
Either you use Selection.SelectionColor or read this:
http://www.biblioscape.com/rtf15_spec.htm#Heading16
-
Jan 26th, 2010, 11:49 AM
#6
Thread Starter
Member
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)
-
Jan 26th, 2010, 11:58 AM
#7
Re: Adding Colored Text To RichTextBox
 Originally Posted by Cooi
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.
-
Jan 26th, 2010, 11:59 AM
#8
Thread Starter
Member
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.
-
Jan 26th, 2010, 12:04 PM
#9
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.
-
Jan 26th, 2010, 12:08 PM
#10
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:
Dim Pos As Integer = rtf.Find("mymatch", 0, RichTextBoxFinds.WholeWord) ' You may use different find options instead of WholeWord Pos+=1 Dim NewPos As Integer = rtf.Find("mymatch", Pos, RichTextBoxFinds.WholeWord) ' etc ' got it?
-
Jan 26th, 2010, 12:10 PM
#11
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|