Bonjour,
autrement il y a les expressions régulières, rapides.
Mais il faut un certain temps pour les assimiler.
Voici un exemple de code :
Sub MisEnFormeSerie(ByVal MonTxt As RichTextBox, ByVal motARechercher As String, _
ByVal emSize As Single, ByVal couLeur As String, ByVal style As FontStyle)
'*** Définit l'expression à rechercher (les "\b" sont des délimiteurs pour
' ne chercher que des mots entiers).
Dim reCherchText As Regex = New Regex("\b" & motARechercher & "\b")
'*** Récupère le premier mot trouvé.
Dim mReCherchText As Match = reCherchText.Match(MonTxt.Text)
'*** Collection de tous les mots cherchés existants dans le texte.
Dim mcReCherchText As MatchCollection = reCherchText.Matches(MonTxt.Text)
'*** Sert à poursuivre la recherche suivant le mot trouvé en cours.
Dim indexReCherchText As Integer
With MonTxt
'*** Tous les mots de la collection seront colorés au choix suivant, avec la police voulue.
For Each mReCherchText In mcReCherchText
.Select(mReCherchText.Index, mReCherchText.Length)
indexReCherchText = mReCherchText.Index + mReCherchText.Length
Select Case couLeur
Case "bleu"
.SelectionFont = New Font("Tahoma", emSize, style)
.SelectionColor = Color.DarkBlue
Case "vert"
.SelectionFont = New Font("Tahoma", emSize, style)
.SelectionColor = Color.ForestGreen
Case "rouge"
.SelectionFont = New Font("Tahoma", emSize, style)
.SelectionColor = Color.Red
Case "violet"
.SelectionFont = New Font("Tahoma", emSize, style)
.SelectionColor = Color.SlateBlue
End Select
Next
End With
End Sub
Ne pas oublier l'import suivant :
Imports System.Text.RegularExpressions
Et pour tester, créer un bouton appelé "btnColorietoutesOccur" et y mettre le code suivant :
Private Sub btnColorietoutesOccur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnColorietoutesOccur.Click
MisEnFormeSerie(rtbCode, "if", 12, "bleu", FontStyle.Bold)
End Sub