2014-12-05 45 views
5

嗨,我有發現從RichTextBox的和改變字體顏色詞的代碼,代碼工作,但IFI回去和編輯以前的文本的東西,我不想顏色,顏色不會消失。這裏是我的代碼RichTextBox中查找和彩色文本的Visual 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 

當我運行通過ВоляАбоСмерть提供的文本的一半不同顏色的被染色的代碼。

enter image description here

+0

'RichTextBox1.SelectAll()''RichTextBox1.SelectionColor = RichTextBox1.ForeColor'爲閃爍煙花來製備。 – LarsTech 2014-12-05 14:53:14

回答

4

編輯:如果你想擴展代碼以允許屬性,修改非常簡單。只要檢查regualr表達式匹配是否包含空格。如果是這樣,那麼查看允許的數組中的匹配,而不考慮屬性,值等。修改代碼並添加圖像。

我知道你要的解決方案,你的方法,但我建議你想要完成的另一種方法。

你可以很容易地解決這個問題,如果你使用Regular Expression.

的想法很簡單.. 在RichTextBox_TextChanged事件,通過所有文字正則表達式匹配製造商迭代,並查找任何HTML標籤(一個開頭不管中間是什麼文字,都可以用<並以>結束。

然後,而不是通過你的陣列中的所有有效HTML標記循環,一個簡單的線條很容易分辨,如果數組Contains元素與否。

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

這裏是我的(測試&工作)代碼..

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:你也可以避免擴大允許的元素你html陣列,通過簡單地使用正則表達式尋找有效的HTML標籤(含標籤,屬性和值之間的空間靈活性等

如果你願意,我可以ELAB在這方面做得更好。

+0

我愛你做了什麼,我怎麼能擴大這個代碼顏色也包含其他標記,如''標籤? – julekgwa 2014-12-15 22:56:28

+0

謝謝,它的工作很棒。 – julekgwa 2014-12-16 07:49:35

0

你其實很接近。將RichTextBox1.SelectionColor = RichTextBox1.ForeColor線路從循環中取出,您就是金。

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