返回首页

您好,我是想突出RichTextBox中的一个单词出现的所有,我试图找到发生的每一个字的位置,它做工精细,但是当我试图强调它只是一大亮点,它突出了另一个事情。这里是代码:

Public sub buscar (Byval cadena as String)

 

        Dim linea As String

        Dim numlineas As Integer

        Dim posicion As Integer = 0

        Dim posicion2 As Integer = 0

        Dim myRegex As New Regex("\w+")

        Dim palabras As MatchCollection

        Dim contador As Integer

        Dim cont As Integer

 

        numlineas = RichTextBox1.Lines.Count() - 1

        posicion = RichTextBox1.Find(cadena)

 

        For contador = 0 To numlineas Step 1

 

            linea = RichTextBox1.Lines(contador)

            cont = 0

            palabras = myRegex.Matches(linea)

 

            

            For Each ele As Match In palabras

                If ele.ToString() = cadena Then

                    

                    ' posicion Gets the position of the element

                    posicion = ele.Index()

 

                    'MsgBox(posicion.ToString()) 

                    

                    'highlight the word

                    RichTextBox1.SelectionStart = posicion

                    RichTextBox1.SelectionLength = cadena.Length()

                    RichTextBox1.SelectionColor = Color.Red

 

                End If

            Next

           

 

        Next

End Sub

回答

评论会员: 时间:2
|