gpt4 book ai didi

vb.net - RichTextBox 查找和颜色文本视觉基础

转载 作者:行者123 更新时间:2023-12-04 15:22:03 25 4
gpt4 key购买 nike

嗨,我有一个用于从 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>", "<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

最佳答案

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

我知道您要求解决您的方法,但我建议另一种方法来完成您想要完成的工作。

如果您使用 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 标签(标签、属性和值等之间的空间具有灵活性)。

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

关于vb.net - RichTextBox 查找和颜色文本视觉基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27317690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com