Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
'--------------------------------------------------------------------------
'
' Goal: Color certain strings within a textfield.
'
' Code found at: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q176643
'
' Steps: Add RichTextBox control on form and name RTB1
' Add Button on form
'
'--------------------------------------------------------------------------
Public Class frmColorText
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents RTB1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.RTB1 = New System.Windows.Forms.RichTextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'RTB1
'
Me.RTB1.Location = New System.Drawing.Point(40, 48)
Me.RTB1.Name = "RTB1"
Me.RTB1.Size = New System.Drawing.Size(440, 24)
Me.RTB1.TabIndex = 2
Me.RTB1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 96)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 24)
Me.Button1.TabIndex = 5
Me.Button1.Text = "Button1"
'
'frmColorText
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(520, 149)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.RTB1})
Me.Name = "frmColorText"
Me.Text = "frmColorText"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RTB1.Text = "skfjdsklfjDUDElksajfdl;kjCOWksjf;lkjsDUDEkjoiuerCOWkljsdf"
FindIt(RTB1, "DUDE", System.Drawing.Color.Blue, 0)
FindIt(RTB1, "COW", System.Drawing.Color.Red, 0)
End Sub
Private Function FindIt(ByRef Box As RichTextBox, ByVal Srch As String, ByVal sColor As System.Drawing.Color, Optional ByVal Start As Long = 0)
'Found at: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q176643
'Orginally written for VB6
Dim retval As Long 'Instr returns a long
Dim Source As String 'variable used in Instr
Source = Box.Text 'put the text to search into the variable
If Start = 0 Then Start = 1 'the initial call doesn't pass a value
'for Start, so it will equal 0
retval = InStr(Start, Source, Srch, CompareMethod.Text) 'do the first search,
'starting at the beginning
'of the text
If retval <> 0 Then 'there is at least one more occurrence of
'the string
'the RichTextBox doesn't support multiple active selections, so
'this section marks the occurrences of the search string by
'making them Bold and Red
With Box
.SelectionStart = retval - 1
.SelectionLength = Len(Srch)
.SelectionColor = sColor
.SelectionLength = 0
End With
Start = retval + Len(Srch) 'move the starting point past the
'first occurrence
'FindIt calls itself with new arguments
'this is what makes it Recursive
FindIt = 1 + FindIt(Box, Srch, sColor, Start)
End If
End Function
End Class