RichTextBox search and color text

Hi, I have a code for searching words from richtextbox and changing the font color, the code works, but I go back and edit the previous text to the fact that I do not want to color, the color does not disappear. 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 Will Abo Death, half of the text is colored in different colors.

enter image description here

+5
source share
2 answers

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

I know that you asked for a solution to your approach, but I recommend a different approach to what you want to accomplish.

You can easily overcome this problem if you use Regular Expression.

The idea is simple. In the RichTextBox_TextChanged event, the regular expression creator iterates over the entire text and looks for any HTML tag (one that starts with < and ends with > ), regardless of the text in between.

Then, instead of iterating over all the valid HTML tags in your array, one simple line can easily determine if the Contains array is an element or not.

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

Here is my (tested and 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 can also not expand the array of valid html elements by simply using a regular expression to search for valid HTML tags (with the flexibility of spaces between tags, properties and values, etc.

If you want, I could elaborate on this.

+4
source

You are actually pretty close. Take the line RichTextBox1.SelectionColor = RichTextBox1.ForeColor from the loop and you will be golden.

 For Each elem As String In html Dim start As Integer = S - elem.Length - 1 If (start >= 0) Then If (RichTextBox1.Text.Substring(start, elem.Length).ToLower.Equals(elem)) Then RichTextBox1.SelectionStart = start RichTextBox1.SelectionLength = elem.Length RichTextBox1.SelectionColor = Color.Green RichTextBox1.SelectionStart = S RichTextBox1.SelectionLength = 0 End If End If Next RichTextBox1.SelectionColor = RichTextBox1.ForeColor 
0
source

Source: https://habr.com/ru/post/1208432/


All Articles