使用 RichTextBox 的 VB.Net 编辑器问题

发布于 2024-11-27 20:37:58 字数 2255 浏览 0 评论 0原文

我使用 RichTextBox 为逻辑程序创建了一个简单的 vb.net 文本编辑器。除了评论之外,我还可以使用着色(突出显示)。然而,在 100 行左右之后,它的运行速度真的非常慢。有谁知道更有效的方法来做到这一点? 注意:我在 RichTextBox TextChanged 事件上调用 SyntaxHandler。

Friend vbKeys As String = "And|As|Case|Catch|CDbl|Ceiling|CInt|Class|Const|Continue|CStr|Decimal|" & _
                          "Default|Delegate|Dim|Do|Double|Each|End|Else|Enum|Event|" & _
                          "Explicit|Extern|False|Finally|Floor|For|Format|GoTo|If|IIf|In|Int|Is|Long|Module|" & _
                          "Namespace|New|Next|Not|Null|Object|Option|Or|Override|Params|PI|Private|Protected|" & _
                          "Public|Readonly|Ref|Replace|Return|Round|Sbyte|Sealed|Select|Short|Sqrt|" & _
                          "Static|String|Structure|Sub|Then|Throw|True|Try|TypeOf|Uint|Ulong|" & _
                          "Unchecked|Using|With|While"

Friend Sub SyntaxHandler(ByVal txtScript As RichTextBox)
    Dim selPos As Integer = txtScript.SelectionStart

    'set everything to black to start with
    txtScript.SelectAll()
    txtScript.SelectionColor = Color.Black

    'Regex Variables for user
    FormatWithRegEx("\b(?:" & regexVaribles & ")\b", txtScript, Color.DarkViolet)

    'double quoted strings are all red
    FormatWithRegEx("""", txtScript, Color.Red)
    FormatWithRegEx("""[^""]*""", txtScript, Color.Red)

    'reserved words are all blue
    FormatWithRegEx("\b(?:" & vbKeys & ")\b", txtScript, Color.Blue)

    'single line comments are all green
    FormatWithRegEx("'[\w*\t*\S*\[ ]*]*", txtScript, Color.Green)

    txtScript.Select(selPos, 0)
    txtScript.SelectionColor = Color.Black

End Sub
Private Sub FormatWithRegEx(ByVal strRegEx As String, ByRef txtRTB As RichTextBox,     ByVal colour As System.Drawing.Color)
    Dim regex As New Regex(strRegEx, _
    RegexOptions.IgnoreCase _
    Or RegexOptions.Multiline _
    Or RegexOptions.Singleline _
    Or RegexOptions.IgnorePatternWhitespace)

    Dim myMatches As MatchCollection = regex.Matches(txtRTB.Text)
    For Each GoodMatch As Match In myMatches
        txtRTB.Select(GoodMatch.Index, GoodMatch.Length)
        txtRTB.SelectionColor = colour
    Next
End Sub

I've created a simple vb.net text editor using a RichTextBox for a Logic Program. I've got the coloring(highlighting) to to work except for comments. However it runs really really slow after a 100 lines or so. Does anyone know of a more efficient way to do this?
Note: I call the SyntaxHandler on the RichTextBox TextChanged event.

Friend vbKeys As String = "And|As|Case|Catch|CDbl|Ceiling|CInt|Class|Const|Continue|CStr|Decimal|" & _
                          "Default|Delegate|Dim|Do|Double|Each|End|Else|Enum|Event|" & _
                          "Explicit|Extern|False|Finally|Floor|For|Format|GoTo|If|IIf|In|Int|Is|Long|Module|" & _
                          "Namespace|New|Next|Not|Null|Object|Option|Or|Override|Params|PI|Private|Protected|" & _
                          "Public|Readonly|Ref|Replace|Return|Round|Sbyte|Sealed|Select|Short|Sqrt|" & _
                          "Static|String|Structure|Sub|Then|Throw|True|Try|TypeOf|Uint|Ulong|" & _
                          "Unchecked|Using|With|While"

Friend Sub SyntaxHandler(ByVal txtScript As RichTextBox)
    Dim selPos As Integer = txtScript.SelectionStart

    'set everything to black to start with
    txtScript.SelectAll()
    txtScript.SelectionColor = Color.Black

    'Regex Variables for user
    FormatWithRegEx("\b(?:" & regexVaribles & ")\b", txtScript, Color.DarkViolet)

    'double quoted strings are all red
    FormatWithRegEx("""", txtScript, Color.Red)
    FormatWithRegEx("""[^""]*""", txtScript, Color.Red)

    'reserved words are all blue
    FormatWithRegEx("\b(?:" & vbKeys & ")\b", txtScript, Color.Blue)

    'single line comments are all green
    FormatWithRegEx("'[\w*\t*\S*\[ ]*]*", txtScript, Color.Green)

    txtScript.Select(selPos, 0)
    txtScript.SelectionColor = Color.Black

End Sub
Private Sub FormatWithRegEx(ByVal strRegEx As String, ByRef txtRTB As RichTextBox,     ByVal colour As System.Drawing.Color)
    Dim regex As New Regex(strRegEx, _
    RegexOptions.IgnoreCase _
    Or RegexOptions.Multiline _
    Or RegexOptions.Singleline _
    Or RegexOptions.IgnorePatternWhitespace)

    Dim myMatches As MatchCollection = regex.Matches(txtRTB.Text)
    For Each GoodMatch As Match In myMatches
        txtRTB.Select(GoodMatch.Index, GoodMatch.Length)
        txtRTB.SelectionColor = colour
    Next
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2024-12-04 20:37:58

一种选择是使用现有的库(例如,Stack Overflow 使用的 prettify)。

One option is to use an existing library (for example, prettify, which is used by Stack Overflow).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文