RichTextBox Change Font Color

HWT

New member
Joined
Nov 9, 2004
Messages
4
Programming Experience
Beginner
Hi,



Can any one help with this…



I have to Richtextbox and I want to change the color of selected strings within the Richtextbox. I am able to do this with the code below. The trouble is if I call the function more than once then all the text in the text box changes to the color blue instead of just the selected string. Does any one know why this occurs or if there is a better way to do this?



Code:



Private sub ColorText(byVal MyString as string)


'Change string "SOMETEXT" to blue in richtextbox1
RichTextBox1.Text = MyString

RichTextBox1.SelectionStart = RichTextBox1.Find("SOMETEXT")

RichTextBox1.SelectionColor = Color.Blue



End sub

Thanks
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
here's a start, you can run it multiple times, but the color never changes back when you select something else

VB.NET:
 Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged
 
 		RichTextBox1.SelectionColor = Color.Blue
 	End Sub
 
Top Bottom