gpt4 book ai didi

VB.NET:语法突出显示

转载 作者:行者123 更新时间:2023-12-02 07:17:37 25 4
gpt4 key购买 nike

我开始学习 VB.NET,并且正在尝试进行语法突出显示。当我设置所选文本的颜色时出现问题。它改变了整个富文本框的内容。

Private Sub txtText_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbText.TextChanged
Dim keywords As ArrayList
Dim index As Integer
Dim keyboardCursorPosition As Integer

keywords = New ArrayList()

keywords.Add(New Keyword("<?php", Color.Red))
keywords.Add(New Keyword("echo", Color.Blue))
keywords.Add(New Keyword("?>", Color.Red))

keyboardCursorPosition = rtbText.SelectionStart

For Each keyword As Keyword In keywords
index = rtbText.Text.IndexOf(keyword.getKey())

If index <> -1 Then
rtbText.Select(index, keyword.getKey().Length)
rtbText.SelectionColor = keyword.getColor()

rtbText.DeselectAll()
rtbText.SelectionStart = keyboardCursorPosition
End If

Next
End Sub

最佳答案

你们已经很接近了。不要忘记恢复 SelectionColor:

    Dim prevColor As Color = rtbText.SelectionColor
For Each keyword As KeyWord In keywords
'' etc...
Next
rtbText.SelectionColor = prevColor

顺便说一句:保持代码干净。 rtb 的消息处理程序不应命名为 txtXxxx。这些小细节迟早会把你搞砸(对我来说就是这样,寻找错误的原因)。还将关键字初始化移出方法。

关于VB.NET:语法突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3121280/

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