2014-12-05 8 views
5

Witam Mam kod do znajdowania słów z richtextbox i zmiany koloru czcionki, kod działa, ale ifi wrócić i edytować poprzedni tekst na coś, co nie chce kolor, kolor nie znika. tutaj jest mój kodRichTextBox znajdowanie i kolorowanie tekstu wizualnego basic

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 

Kiedy uruchomić kod podany przez Воля Або Смерть połowę tekstu jest barwione w różnych kolorach.

enter image description here

+0

'RichTextBox1.SelectAll()' 'RichTextBox1.SelectionColor = RichTextBox1.ForeColor' Przygotuj się na migoczące fajerwerki. – LarsTech

Odpowiedz

4

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.

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

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.

+0

Uwielbiam to, co zrobiłeś, w jaki sposób mogę rozszerzyć ten kod na tagi kolorowe zawierające inne znaczniki, takie jak ''? – julekgwa

+0

dzięki, działa świetnie. – julekgwa

0

Jesteś rzeczywiście bardzo blisko. Wyjmij linię RichTextBox1.SelectionColor = RichTextBox1.ForeColor z pętli, a otrzymasz złoty kolor.

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