I have this richtextbox and displayed various text from .xml database. I need to format the text. For example when ever my vb code find string like 'the' in an .xml line it should display in italic as well as in blue color and so on for some other specific text. Is there anyway I could do this. Any help would be highly appreciated.

Thank you.

Zela

Recommended Answers

All 3 Replies

Just find the text you need to change the font or the color using the Find function of the rich text box, wich will return the text position, or advance through the text property analyzing the current text (without the need to be aware of the curent format).
Then just use the Select function to select the text you want to modify.
Using the SelectionFont and SelectionColor properties you will achieve the expected result.

Hope this helps

Would be grateful if a sample code is given.

Thanks.

'
		'	Having a rich text box
		'
		Dim Rt As New RichTextBox
		'
		'	Let some example text
		'
		Rt.Text = "This is some kind of text to analyze for any occurrence of the word is."
		'
		'	Search fot the 'is' word
		'
		Dim StartSearchPosition As Integer = 0
		Dim SearchString As String = "is"
		Dim p As Integer = Rt.Find(SearchString, StartSearchPosition, RichTextBoxFinds.WholeWord Or RichTextBoxFinds.NoHighlight Or RichTextBoxFinds.MatchCase)
		'
		'	If some thing found
		'
		Do While p >= 0
			'
			'	Here is the 'is' word
			'
			Rt.Select(p, SearchString.Length)
			'
			'	Set it to italic
			'
			Rt.SelectionFont = New System.Drawing.Font(Rt.SelectionFont, Drawing.FontStyle.Italic)
			'
			'	Set it to blue
			'
			Rt.SelectionColor = Drawing.Color.Blue
			'
			'	Set the new Start search position
			'
			StartSearchPosition += p + SearchString.Length
			'
			'	Search the if there is another 'is'
			'
			p = Rt.Find(SearchString, StartSearchPosition, RichTextBoxFinds.WholeWord Or RichTextBoxFinds.NoHighlight Or RichTextBoxFinds.MatchCase)
			'
		Loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.