vb.net – RichTextBox查找和颜色文本visual basic

前端之家收集整理的这篇文章主要介绍了vb.net – RichTextBox查找和颜色文本visual basic前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我有一个代码,用于从richtextBox中查找单词并更改字体颜色,代码正在工作但我回去编辑以前的文本到我不想着色的东西,颜色不会消失.这是我的代码
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>","<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

当我运行ВоляАбоСмерть提供的代码时,一半的文本用不同的颜色着色.

编辑:如果要扩展代码以允许属性,修改非常简单.只需检查regualr表达式匹配是否包含空格.如果是这样,那么在允许的数组中查找匹配,而不考虑属性,值等.代码修改,并添加了图像.

我知道你要求解决你的方法,但我建议你想要实现的另一种方法.

如果您使用Regular Expression.,则可以轻松解决此问题

这个想法很简单..
在RichTextBox_TextChanged事件中,正则表达式匹配生成器遍历所有文本并查找任何HTML标记(以<开头并以>结尾的标记),而不管其间的文本.

然后,不是循环遍历数组中的所有有效HTML标记,而是一条简单的行可以轻松判断数组是否包含该元素.

这是我的(测试和工作)代码..

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>","<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数组.

如果你愿意,我可以详细说明这一点.

猜你在找的VB相关文章