Basicamente a tu código le falta usar la función
RichTextBox.Find() junto a un búcle para ir iterando las posiciones de las ocurrencias encontradas en el texto. Lo que has intentado hacer recurriendo al uso de funciones de VB6 como
InStr... ese no es el camino en .NET, debes evitar toda esa morralla (basura) de miembros de VB6, ya que son miembros que están ahí solo por compatibilidad, aunque estén escritos en código .NET son miembros cuyo código fuente es muy poco óptimo y limitado, todo esto ya te lo comenté en el pasado pero sigues sin hacer caso del consejo. :-/
Una solucoón que considero sencilla de aplicar para cualquier nivel de aprendizaje, podría ser la siguiente, a modo de función de uso genérico reutilizable para cualquier tipo de ocasión...
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Find all the occurrences of the specified strings in the source <see cref="RichTextBox"/>
''' and set the foreground color, background color, and the font of any occurrence found.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="rtb">
''' The source <see cref="RichTextBox"/>.
''' </param>
'''
''' <param name="find">
''' An Array containing the strings to match.
''' </param>
'''
''' <param name="ignoreCase">
''' Specifies how a text search is carried out.
''' </param>
'''
''' <param name="foreColor">
''' The foreground color to set for the matched strings.
''' </param>
'''
''' <param name="backColor">
''' The background color to set for the matched strings.
''' </param>
'''
''' <param name="font">
''' The font to set for the matched strings.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' Returns the total amount of occurrences found.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Function InternalColorizeMatches(ByVal rtb As RichTextBox,
ByVal find As String,
ByVal ignoreCase As Boolean,
ByVal foreColor As Color,
ByVal backColor As Color,
ByVal font As Font) As Integer
If String.IsNullOrEmpty(find) Then
Return 0
End If
' Set letter-case criteria.
Dim richTextBoxFinds As RichTextBoxFinds =
If(ignoreCase, RichTextBoxFinds.None, RichTextBoxFinds.MatchCase)
Dim stringComparison As StringComparison =
If(ignoreCase, StringComparison.OrdinalIgnoreCase, StringComparison.Ordinal)
' Save the current caret position to restore it at the end.
Dim caretPosition As Integer = rtb.SelectionStart
Dim successCount As Integer = 0
Dim textLength As Integer = rtb.TextLength
Dim firstIndex As Integer = 0
Dim lastIndex As Integer = rtb.Text.LastIndexOf(find, stringComparison)
While (firstIndex <= lastIndex)
Dim findIndex As Integer = rtb.Find(find, firstIndex, textLength, richTextBoxFinds)
If (findIndex <> -1) Then
successCount += 1
Else
Continue While
End If
rtb.SelectionColor = foreColor
rtb.SelectionBackColor = backColor
rtb.SelectionFont = font
firstIndex = (rtb.Text.IndexOf(find, findIndex, stringComparison) + 1)
End While ' (firstIndex <= lastIndex)
' Restore the caret position. Reset selection length to zero.
rtb.Select(caretPosition, length:=0)
Return successCount
End Function
Ese código de arriba resolvería el problema, con eso ya está, así que si lo prefieres puedes no seguir leyendo nada más de este comentario, pero si quieres aprender cosas nuevas entonces sigue leyendo...
...Para implementar las funcionalidades de búsqueda y resaltado de palabras, para hacerlo decentemente quiero decir, lo más apropiado sería empezar por bloquear temporálmente los mensajes de ventana de dibujado de la ventana del control afectado para optimizar (acelerar) el procedimiento de ejecución, y de paso al mismo tiempo intentar evitar indiseados efectos de
flickering.
Bueno, ya que lo has intentado hacer por ti mismo, te voy a mostrar un ejemplo completo y funcional. Para hacerlo funcionar simplemente debes copia y pegar cada bloque de código que iré mostrando, en una nueva clase por cada bloque de código (pero no seas vago, lee algo del código para intentar aprender buenos hábitos de programación .NET). Si no entiendes algo, pregúntalo.
Debido al límite de caracteres del foro, me he visto obligado a recortar mucho código para eliminar practicamente casi toda la documentación XML...
Sin embargo, para hacerlo todo más facil y comprensible,
abajo del todo de esta respuesta te dejo un proyecto hecho en Visual Studio 2017 con el código y su documentación al completo y que además contiene esta pequeña aplicación para demostrar la funcionalidad de búsqueda y resaltado de palabras:
También cabe mencionar que el código fuente de aquí abajo solo provee la funcionalidad de "
Buscar todas las ocurrencias", pero no provee la funcionalidad de "
Buscar siguiente palabra" hacia arriba/abajo etc, aunque si que tengo implementadas ese tipo de funciones en mi librería comercial... pero tampoco voy a regalarlo todo hecho, así que me limito a resolver la duda en concreto de "
¿cómo buscar y colorear todas las ocurrencias de un string?".
NOTA INFORMATIVA:
---
EL SIGUIENTE CÓDIGO HA SIDO EXTRAIDO Y OFRECIDO DE FORMA GRATUITA A PARTIR DE MI FRAMEWORK COMERCIAL ELEKTROKIT FRAMEWORK , EL CUAL CONTIENE UNA INFINIDAD DE UTILIDADES ENFOCADAS A UNA AMPLIA VARIEDAD DE TEMÁTICAS Y ESCENARIOS EN LA PROGRAMACIÓN .NET, COMO ÉSTE. SI QUIEREN CONOCER MÁS ACERCA DEL PRODUCTO, PUEDEN ENCONTRARLO EN MI FIRMA DE USUARIO DEL FORO.
ESTE CÓDIGO SE PUEDE USAR Y MODIFICAR DE FORMA LIBRE COMO LES APETEZCA.
---
WindowsMessages.vb#Region " Windows Messages "
Namespace ElektroKit.Interop.Win32
' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927%28v=vs.85%29.aspx
Friend Enum WindowsMessages As Integer
' http://msdn.microsoft.com/en-us/library/windows/desktop/dd145219%28v=vs.85%29.aspx
WM_SetRedraw = &HB
End Enum
End Namespace
#End Region
RedrawWindowFlags.vb#Region " RedrawWindowFlags "
Namespace ElektroKit.Interop.Win32
' http://msdn.microsoft.com/en-us/library/windows/desktop/dd162911%28v=vs.85%29.aspx
<Flags>
Friend Enum RedrawWindowFlags As Integer
Invalidate = &H1
InternalPaint = &H2
[Erase] = &H4
Validate = &H8
NoInternalPaint = &H10
NoErase = &H20
NoChildren = &H40
AllChildren = &H80
UpdateNow = &H100
EraseNow = &H200
Frame = &H400
NoFrame = &H800
End Enum
End Namespace
#End Region
NativeMethods.vb#Region " Imports "
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Security
#End Region
#Region " NativeMethods "
Namespace ElektroKit.Interop.Win32
' http://msdn.microsoft.com/en-us/library/ms182161.aspx
Friend NotInheritable Class NativeMethods ' NOT <SuppressUnmanagedCodeSecurity>
#Region " Constructors "
<DebuggerNonUserCode>
Private Sub New()
End Sub
#End Region
#Region " User32.dll "
' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
<DllImport("User32.dll", SetLastError:=True)>
Friend Shared Function SendMessage(ByVal hwnd As IntPtr,
ByVal msg As Integer,
ByVal wParam As IntPtr,
ByVal lParam As IntPtr
) As IntPtr
End Function
' http://msdn.microsoft.com/en-us/library/windows/desktop/dd162911%28v=vs.85%29.aspx
<DllImport("User32.dll")>
Friend Shared Function RedrawWindow(ByVal hwnd As IntPtr,
ByVal lprcUpdate As IntPtr,
ByVal hrgnUpdate As IntPtr,
ByVal flags As RedrawWindowFlags
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
#End Region
End Class
End Namespace
#End Region
IWin32Window_Extensions.vb#Region " Imports "
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.Runtime.CompilerServices
Imports ElektroKit.Interop.Win32
Imports WinForms = System.Windows.Forms
#End Region
#Region " IWin32Window Extensions "
Namespace ElektroKit.Extensions.[IWin32Window]
''' <summary>
''' Provides custom extension methods to use with <see cref="WinForms.IWin32Window"/> type.
''' </summary>
<HideModuleName>
Public Module Drawing
#Region " Public Extension Methods "
''' <summary>
''' Prevents the specified window from being redrawn.
''' <para></para>
''' By calling this method, it will disallow painting events from firing on the specified window.
''' </summary>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Sub SuspendDrawing(ByVal sender As WinForms.IWin32Window)
NativeMethods.SendMessage(sender.Handle, WindowsMessages.WM_SetRedraw, IntPtr.Zero, IntPtr.Zero)
End Sub
''' <summary>
''' Allow the specified window to be redrawn.
''' <para></para>
''' By calling this method, it will allow painting events to be fired on the specified window.
''' </summary>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Sub ResumeDrawing(ByVal sender As WinForms.IWin32Window)
Drawing.ResumeDrawing(sender, True)
End Sub
''' <summary>
''' Allow the specified window to be redrawn.
''' <para></para>
''' By calling this method, it will allow painting events to be fired on the specified window.
''' </summary>
''' <param name="redraw">
''' If set to <see langword="True"/>, causes the window to de redrawn
''' (similarly as calling <see cref="WinForms.Control.Refresh()"/> method).
''' </param>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Sub ResumeDrawing(ByVal sender As WinForms.IWin32Window, ByVal redraw As Boolean)
NativeMethods.SendMessage(sender.Handle, WindowsMessages.WM_SetRedraw, New IntPtr(1), IntPtr.Zero)
If (redraw) Then
NativeMethods.RedrawWindow(sender.Handle, IntPtr.Zero, IntPtr.Zero,
RedrawWindowFlags.Frame Or
RedrawWindowFlags.UpdateNow Or
RedrawWindowFlags.Invalidate)
End If
End Sub
#End Region
End Module
End Namespace
#End Region
RichTextBox_Extensions.vb#Region " Imports "
Imports System.ComponentModel
Imports System.Drawing
Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions
Imports WinForms = System.Windows.Forms
#End Region
#Region " RichTextBox Extensions "
Namespace ElektroKit.Extensions.[RichTextBox]
''' <summary>
''' Provides custom extension methods to use with <see cref="WinForms.RichTextBox"/> control.
''' </summary>
<HideModuleName>
Public Module WordFinding
#Region " Public Extension Methods "
#Region " Strings "
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As String, ByVal ignoreCase As Boolean,
ByVal foreColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, {find}, ignoreCase, foreColor, Nothing, Nothing)
End Function
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As String, ByVal ignoreCase As Boolean,
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, {find}, ignoreCase, foreColor, backColor, Nothing)
End Function
''' <summary>
''' Matches all the occurrences of the specified string in the source <see cref="WinForms.RichTextBox"/>
''' and set the foreground color, background color, and the font of any occurrence found.
''' </summary>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As String, ByVal ignoreCase As Boolean,
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color,
ByVal font As Font) As Integer
Return WordFinding.InternalColorizeMatches(sender, {find}, ignoreCase, foreColor, backColor, font)
End Function
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As String(), ByVal ignoreCase As Boolean,
ByVal foreColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, find, ignoreCase, foreColor, Nothing, Nothing)
End Function
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As String(), ByVal ignoreCase As Boolean,
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, find, ignoreCase, foreColor, backColor, Nothing)
End Function
''' <summary>
''' Matches all the occurrences of the specified strings in the source <see cref="WinForms.RichTextBox"/>
''' and set the foreground color, background color, and the font of any occurrence found.
''' </summary>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As String(), ByVal ignoreCase As Boolean,
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color,
ByVal font As Font) As Integer
Return WordFinding.InternalColorizeMatches(sender, find, ignoreCase, foreColor, backColor, font)
End Function
#End Region
#Region " Regular Expressions "
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex,
ByVal foreColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, {find}, foreColor, Nothing, Nothing)
End Function
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex,
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, {find}, foreColor, backColor, Nothing)
End Function
''' <summary>
''' Matches all the occurrences of the specified regular expression in the source <see cref="WinForms.RichTextBox"/>
''' and set the foreground color, background color, and the font of any occurrence found.
''' </summary>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex,
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color,
ByVal font As Font) As Integer
Return WordFinding.InternalColorizeMatches(sender, {find}, foreColor, backColor, font)
End Function
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex(),
ByVal foreColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, find, foreColor, Nothing, Nothing)
End Function
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex(),
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color) As Integer
Return WordFinding.InternalColorizeMatches(sender, find, foreColor, backColor, Nothing)
End Function
''' <summary>
''' Matches all the occurrences of any of the specified regular expressions in the source <see cref="WinForms.RichTextBox"/>
''' and set the foreground color, background color, and the font of any occurrence found.
''' </summary>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function ColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex(),
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color,
ByVal font As Font) As Integer
Return WordFinding.InternalColorizeMatches(sender, find, foreColor, backColor, font)
End Function
''' <summary>
''' Matches all the occurrences of the specified regular expression in the source <see cref="WinForms.RichTextBox"/>
''' and invokes the specified action for any occurrence found.
''' </summary>
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Sub IterateMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex,
ByVal action As Action(Of WinForms.RichTextBox, Match))
WordFinding.InternalIterateMatches(sender, {find}, action)
End Sub
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Sub IterateMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex(),
ByVal action As Action(Of WinForms.RichTextBox, Match))
WordFinding.InternalIterateMatches(sender, find, action)
End Sub
#End Region
#End Region
#Region " Private Methods "
''' <summary>
''' Find all the occurrences of the specified strings in the source <see cref="WinForms.RichTextBox"/>
''' and set the foreground color, background color, and the font of any occurrence found.
''' </summary>
''' <returns>
''' Returns the total amount of occurrences found.
''' </returns>
<DebuggerStepThrough>
Private Function InternalColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As String(), ByVal ignoreCase As Boolean,
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color,
ByVal font As Font) As Integer
If (foreColor = Nothing) OrElse (foreColor = Color.Empty) Then
foreColor = sender.ForeColor
End If
If (backColor = Nothing) OrElse (backColor = Color.Empty) Then
backColor = sender.BackColor
End If
If (font Is Nothing) Then
font = sender.Font
End If
' Set letter-case criteria.
Dim richTextBoxFinds As RichTextBoxFinds =
If(ignoreCase, RichTextBoxFinds.None, RichTextBoxFinds.MatchCase)
Dim stringComparison As StringComparison =
If(ignoreCase, StringComparison.OrdinalIgnoreCase, StringComparison.Ordinal)
' Save the current caret position to restore it at the end.
Dim caretPosition As Integer = sender.SelectionStart
' Suspend the control layout logic. And suspend painting events from firing.
sender.SuspendLayout()
ElektroKit.Extensions.IWin32Window.SuspendDrawing(sender)
' Colorize the matches.
Dim successCount As Integer = 0
Dim textLength As Integer = sender.TextLength
For Each s As String In find
If String.IsNullOrEmpty(s) Then
Continue For
End If
Dim firstIndex As Integer = 0
Dim lastIndex As Integer = sender.Text.LastIndexOf(s, stringComparison)
While (firstIndex <= lastIndex)
Dim findIndex As Integer = sender.Find(s, firstIndex, textLength, richTextBoxFinds)
If (findIndex <> -1) Then
successCount += 1
Else
Continue While
End If
sender.SelectionColor = foreColor
sender.SelectionBackColor = backColor
sender.SelectionFont = font
firstIndex = (sender.Text.IndexOf(s, findIndex, stringComparison) + 1)
End While ' (firstIndex <= lastIndex)
Next s
' Restore the caret position. Reset selection length to zero.
sender.Select(caretPosition, length:=0)
' Restore the control layout logic. And resume painting events.
sender.ResumeLayout()
ElektroKit.Extensions.IWin32Window.ResumeDrawing(sender, redraw:=True)
Return successCount
End Function
''' <summary>
''' Find all the occurrences of the specified regular expressions in the source <see cref="WinForms.RichTextBox"/>
''' and set the foreground color, background color, and the font of any occurrence found.
''' </summary>
''' <returns>
''' Returns the total amount of occurrences found.
''' </returns>
<DebuggerStepThrough>
Private Function InternalColorizeMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex(),
ByVal foreColor As Global.System.Drawing.Color,
ByVal backColor As Global.System.Drawing.Color,
ByVal font As Font) As Integer
If (foreColor = Nothing) OrElse (foreColor = Color.Empty) Then
foreColor = sender.ForeColor
End If
If (backColor = Nothing) OrElse (backColor = Color.Empty) Then
backColor = sender.BackColor
End If
If (font Is Nothing) Then
font = sender.Font
End If
' Save the current caret position to restore it at the end.
Dim caretPosition As Integer = sender.SelectionStart
' Suspend the control layout logic. And suspend painting events from firing.
sender.SuspendLayout()
ElektroKit.Extensions.IWin32Window.SuspendDrawing(sender)
' Colorize the matches.
Dim successCount As Integer = 0
For Each rgx As Regex In find
Dim matches As MatchCollection = rgx.Matches(sender.Text, rgx.Options)
successCount += matches.Count
For Each m As Match In matches
sender.Select(m.Index, m.Length)
sender.SelectionColor = foreColor
sender.SelectionBackColor = backColor
sender.SelectionFont = font
Next m
Next rgx
' Restore the caret position. Reset selection length to zero.
sender.Select(caretPosition, length:=0)
' Restore the control layout logic. And resume painting events.
sender.ResumeLayout()
ElektroKit.Extensions.IWin32Window.ResumeDrawing(sender, redraw:=True)
Return successCount
End Function
''' <summary>
''' Matches all the occurrences of the specified regular expression in the source <see cref="WinForms.RichTextBox"/>
''' and invokes the specified action for any occurrence found.
''' </summary>
<DebuggerStepThrough>
Private Sub InternalIterateMatches(ByVal sender As WinForms.RichTextBox,
ByVal find As Regex(),
ByVal action As Action(Of WinForms.RichTextBox, Match))
' Suspend the control layout logic. And suspend painting events from firing.
sender.SuspendLayout()
ElektroKit.Extensions.IWin32Window.SuspendDrawing(sender)
' Iterate the matches.
For Each rgx As Regex In find
Dim matches As MatchCollection = rgx.Matches(sender.Text, rgx.Options)
For Each m As Match In matches
action.Invoke(sender, m)
Next m
Next rgx
' Restore the control layout logic. And resume painting events.
sender.ResumeLayout()
ElektroKit.Extensions.IWin32Window.ResumeDrawing(sender, redraw:=True)
End Sub
#End Region
End Module
End Namespace
#End Region
EJEMPLOS DE USO REAL-WORLDForm1.vbClass Form1
' ...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rtb As RichTextBox = Me.RichTextBox1
' Reset selection.
With rtb
.SelectAll()
.SelectionColor = .ForeColor
.SelectionBackColor = .BackColor
.SelectionFont = .Font
.Select(0, 0)
End With
' Perform a new selection.
Dim find As String = Me.TextBox1.Text ' The text to find.
Dim ignoreCase As Boolean = Me.CheckBox1.Checked
Dim forecolor As Color = Color.LimeGreen
Dim backcolor As Color = rtb.SelectionBackColor
Dim font As Font = rtb.SelectionFont
Dim occurrences As Integer = rtb.ColorizeMatches(find, ignoreCase, forecolor, backcolor, font)
Me.Label1.Text = String.Format("{0} occurrences", occurrences)
End Sub
' ...
End Class
EJEMPLOS DE USO DE LAS SOBRECARGAS PARA STRINGDim find As String = "Hello World"
Dim forecolor As Color = Color.Red
RichTextBox1.ColorizeMatches(find, True, forecolor)
Dim find As String = "Hello World"
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
RichTextBox1.ColorizeMatches(find, True, forecolor, backcolor)
Dim find As String = "Hello World"
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
Dim font As New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic)
RichTextBox1.ColorizeMatches(find, True, forecolor, backcolor, font)
Dim find As String() = {"Hello", "World"}
Dim forecolor As Color = Color.Red
RichTextBox1.ColorizeMatches(find, True, forecolor)
Dim find As String() = {"Hello", "World"}
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
RichTextBox1.ColorizeMatches(find, True, forecolor, backcolor)
Dim find As String() = {"Hello", "World"}
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
Dim font As New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic)
RichTextBox1.ColorizeMatches(find, True, forecolor, backcolor, font)
EJEMPLOS DE USO DE LAS SOBRECARGAS PARA EXPRESIONES REGULARESDim find As New Regex("[0-9]", RegexOptions.None)
Dim forecolor As Color = Color.Red
RichTextBox1.ColorizeMatches(rgx, forecolor)
Dim find As New Regex("[0-9]", RegexOptions.None)
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
RichTextBox1.ColorizeMatches(rgx, forecolor, backcolor)
Dim find As New Regex("[0-9]", RegexOptions.None)
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
Dim font As New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic)
RichTextBox1.ColorizeMatches(rgx, forecolor, backcolor, font)
Dim rgx1 As New Regex("[0-9]", RegexOptions.None)
Dim rgx2 As New Regex("[a-z]", RegexOptions.None)
Dim forecolor As Color = Color.Red
RichTextBox1.ColorizeMatches({rgx1, rgx2}, forecolor)
Dim rgx1 As New Regex("[0-9]", RegexOptions.None)
Dim rgx2 As New Regex("[a-z]", RegexOptions.None)
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
RichTextBox1.ColorizeMatches({rgx1, rgx2}, forecolor, backcolor)
Dim rgx1 As New Regex("[0-9]", RegexOptions.None)
Dim rgx2 As New Regex("[a-z]", RegexOptions.None)
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
Dim font As New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic)
RichTextBox1.ColorizeMatches({rgx1, rgx2}, forecolor, backcolor, font)
Dim rgx As New Regex("[0-9]", RegexOptions.None)
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
Dim font As New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic)
Dim action As New Action(Of RichTextBox, Match)(
Sub(rtb As RichTextBox, m As Match)
With rtb
.Select(m.Index, m.Length)
.SelectionColor = forecolor
.SelectionBackColor = backcolor
.SelectionFont = font
End With
End Sub)
RichTextBox1.IterateMatches(rgx, action)
Dim rgx1 As New Regex("[0-9]", RegexOptions.None)
Dim rgx2 As New Regex("[a-z]", RegexOptions.None)
Dim forecolor As Color = Color.Red
Dim backcolor As Color = Color.Black
Dim font As New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic)
Dim action As New Action(Of RichTextBox, Match)(
Sub(rtb As RichTextBox, m As Match)
With rtb
.Select(m.Index, m.Length)
.SelectionColor = forecolor
.SelectionBackColor = backcolor
.SelectionFont = font
End With
End Sub)
RichTextBox1.IterateMatches({rgx1, rgx2}, action)
SOLUCIÓN PARA VISUAL STUDIO 2017( contiene todo el código fuente de arriba, documentado al completo. )
Nota: los colores de texto y fondo de los controles no se verán igual que en la imagen GIF de arriba, así que es probable que necesiten ajustar dichos colores.