Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RichTextBox find and color text visual basic

Tags:

vb.net

Hi i have a code for finding words from richtextbox and change font color, the code is working but i f i go back and edit the previous text to something that i don't want to color, the color doesn't go away. here is my code

Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    Dim S As Integer = RichTextBox1.SelectionStart
    Dim html() As String = {"<!DOCTYPE html>", "<html>", "</html>", "<head>", "</head>", "<body>", "</body>", "pre>", "</pre>", "<!DOCTYPE>", "<title>", "</title>", "<a>",
                            "<abbr>", "<address>", "<area>", "<article>", "<aside>", "<audio>", "<acronym>", "<applet>", "<b>", "<base>", "<bdi>", "<bdo>", "<blockquote>", "<body>", "<br>", "<button>", "<basefont>", "<bgsound>", "<big>", "<blink>"}
    For i As Integer = 0 To html.Length - 1
        Dim str As String = html(i)
        Dim start As Integer = S - str.Length - 1
        If (start >= 0) Then
            If (RichTextBox1.Text.Substring(start, str.Length).ToLower.Equals(str)) Then
                RichTextBox1.SelectionStart = start
                RichTextBox1.SelectionLength = str.Length
                RichTextBox1.SelectionColor = Color.Green
                RichTextBox1.SelectionStart = S
                RichTextBox1.SelectionLength = 0

            End If
        End If
    Next
    RichTextBox1.SelectionColor = RichTextBox1.ForeColor
End Sub

When i run the code provided by Воля Або Смерть the half of text is colored in different colors.

enter image description here

like image 987
Junius L. Avatar asked Dec 05 '14 14:12

Junius L.


1 Answers

EDITED: if you want to extend the code to allow properties, the modification is very simple. Just check if the regualr expression match contains a space or not. If so, then look in the allowed array for the match without any regards to the properties, values, etc. Code modified, and image added.

I know you asked for solution to your approach, but I am advising another approach for what you want to accomplish.

You could easily overcome this problem if you used Regular Expression.

The idea is simple.. At the RichTextBox_TextChanged event, a regular expression match maker iterates through all text and looks for any HTML tag (one that begins with < and ends with >) regardless of the text in-between.

Then instead of looping through all valid HTML tags in your array, one simple line can easily tell if the array Contains the element or not.

Sample Screenshot : without properties, valuesSample Screenshot : with properties, and values

Here is my (Tested & Working) Code..

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RichTextBox1.TextChanged


        Dim current_cursor_position As Integer = Me.RichTextBox1.SelectionStart
        'This is useful to get a hold of where is the current cursor at
        'this will be needed once all coloring is done, and we need to return 



       Dim html() As String = {"<!DOCTYPE html>", "<html>", "</html>", "<head>", "</head>",
                            "<body>", "</body>", "pre>", "</pre>", "<!DOCTYPE>", "<title>",
                            "</title>", "<a>", "<abbr>", "<address>", "<area>", "<article>",
                            "<aside>", "<audio>", "<acronym>", "<applet>", "<b>", "<base>",
                            "<bdi>", "<bdo>", "<blockquote>", "<body>", "<br>", "<button>",
                            "<basefont>", "<bgsound>", "<big>", "<blink>", "<img>","</img>",
                            "<input>","</input>"}

        Dim pattern As String = "<(.)*?>"
        Dim matches As MatchCollection = Regex.Matches(Me.RichTextBox1.Text, pattern)
        For Each match In matches
            Me.RichTextBox1.Select(match.index, match.length)

            Dim lookFor As String = match.ToString

            If match.ToString.Contains(" ") Then   'Checking if tag contains properties

                 lookFor = match.ToString.Substring(0, match.ToString.IndexOf(" ")) & ">"
                    'This line will strip away any extra properties, values, and will
                    ' close up the tag to be able to look for it in the allowed array
            End If

            If html.Contains(lookFor.ToString.ToLower) Then
                'The tag is part of the allowed tags, and can be colored green.
                Me.RichTextBox1.SelectionColor = Color.Green
            Else
                'This tag is not recognized, and shall be colored black..
                Me.RichTextBox1.SelectionColor = Color.Black
            End If

        Next

        Me.RichTextBox1.SelectionStart = current_cursor_position
                                        'Returning cursor to its original position

        Me.RichTextBox1.SelectionLength = 0
                                         'De-Selecting text (if it was selected)


        Me.RichTextBox1.SelectionColor = Color.Black
                                         'new text will be colored black, until 
                                         'recognized as HTML tag.

    End Sub
End Class

PS: you could also avoid expanding your html array of allowed elements, by simply using a regular expression to look for valid HTML tags (with flexibility of spaces between tags, properties and values, etc.

If you wish, I could elaborate on this.

like image 94
Ahmad Avatar answered Sep 22 '22 04:09

Ahmad