我開始學習VB.NET,我試着做一個語法高亮。當我設置選定文本的顏色時,會發生問題。它改變了整個richtextbox的內容。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
啊,我以爲是這樣,但我決定在發佈答案之前嘗試一下,我不明白爲什麼它不起作用(出於某種原因,我把'rtbText.SelectionColor = prevColor'放在'DeselectAll' ...),謝謝讓我感覺不那麼困惑:) – 2010-06-25 21:07:58