RSS

Highlight specific text in RichTextBox

18 Jun

TextBox is not capable of displaying text with multiple colors. We need to use RichTextBox in order to highlight specific text. The demo code here uses .NET Regular Expression to find the text and highlight them with red color.

Include this reference to use Regex class.

Imports System.Text.RegularExpressions

Highlight 8-digit numbers in the RichTextBox.

Dim regex As New Regex("[0-9]{8}")

For Each matchedText As Match In regex.Matches(Me.RichTextBoxApprovedVoucher.Text)
    Me.RichTextBox1.Select(matchedText.Index, matchedText.Length)
    Me.RichTextBox1.SelectionColor = Color.Red
Next
Advertisements
 
Leave a comment

Posted by on June 18, 2012 in .NET, VB, Winform

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: