VB.NET Technology

VB.Net Projects

VB.Net Project 1

How to Change The Color of Specific Word in Rich textbox using VB.Net
Previous Home Next
adplus-dvertising

.vbCode

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
    Private Shared Sub HighlightPhrase(ByVal box As RichTextBox, 
	ByVal phrase As String, ByVal color As Color)
        Dim pos As Integer = box.SelectionStart
        Dim s As String = box.Text
        Dim ix As Integer = 0
        While True
            Dim jx As Integer = s.IndexOf(phrase, ix, 	
		StringComparison.CurrentCultureIgnoreCase)
            If jx < 0 Then
                Exit While
            End If
            box.SelectionStart = jx
            box.SelectionLength = phrase.Length
            box.SelectionColor = color
            ix = jx + 1
        End While
        box.SelectionStart = pos
        box.SelectionLength = 0
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button1.Click
        HighlightPhrase(RichTextBox1, "a", Color.Red)
        HighlightPhrase(RichTextBox1, "b", Color.Blue)
    End Sub
End Class
Output :
Previous Home Next