EDYCJA: jeśli chcesz przedłużyć kod, aby umożliwić właściwości, modyfikacja jest bardzo prosta. Sprawdź, czy dopasowanie wyrażenia regualr zawiera spację, czy nie. Jeśli tak, to spójrz na dozwoloną tablicę w celu dopasowania bez względu na właściwości, wartości itp. Zmodyfikowany kod i obraz został dodany.
Wiem, że prosiłeś o rozwiązanie swojego podejścia, ale doradzam inne podejście do tego, co chcesz osiągnąć.
Można łatwo rozwiązać ten problem, jeśli używany Regular Expression.
Pomysł jest prosty .. Na razie RichTextBox_TextChanged
, regularne iteracje ekspres wyrażenie mecz przez wszystkie tekstu i wygląda na każdym tagu HTML (jeden zaczynający się <
i kończy się na >
) niezależnie od tekstu pomiędzy.
Zamiast tego, zamiast zapętlać wszystkie ważne znaczniki HTML w tablicy, jeden prosty wiersz może łatwo stwierdzić, czy tablica jest elementem, czy nie.
Oto moje (Testowany & roboczy) Kod ..
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: można również uniknąć rozszerza swoją tablicę html
dozwolonych elementów, po prostu za pomocą wyrażenia regularnego szukaj prawidłowych znaczników HTML (z dużą ilością spacji między znacznikami, właściwościami i wartościami itp.).
Jeśli chcesz, mogę w tym celu.
'RichTextBox1.SelectAll()' 'RichTextBox1.SelectionColor = RichTextBox1.ForeColor' Przygotuj się na migoczące fajerwerki. – LarsTech