我正在爲我們在工作中使用的非常簡單的腳本語言編寫簡單的代碼編輯器。我的語法突出顯示代碼工作正常,如果我在整個RichTextBox
(rtbMain
)上執行它,但是當我試圖讓它在該行上工作時,所以我可以使用rtbMain
更改運行該函數,它變得很奇怪。我似乎無法弄清楚爲什麼。我甚至會以正確的方式去做這件事嗎?在富文本框中突出顯示VB.NET語法
rtbMain
是主要的文本框。 frmColors.lbRegExps
是要突出顯示的單詞的列表框(稍後它將具有稍微更強大的正則表達式。) frmColor.lbHexColors
是另一個具有對應單詞的單詞列表框。
Private Sub HighLight(ByVal All As Boolean)
Dim RegExp As System.Text.RegularExpressions.MatchCollection
Dim RegExpMatch As System.Text.RegularExpressions.Match
Dim FirstCharIndex As Integer = rtbMain.GetFirstCharIndexOfCurrentLine
Dim CurrentLine As Integer = rtbMain.GetLineFromCharIndex(FirstCharIndex)
Dim CurrentLineText As String = rtbMain.Lines(CurrentLine)
Dim CharsToCurrentLine As Integer = rtbMain.SelectionStart
Dim PassNumber As Integer = 0
LockWindowUpdate(Me.Handle.ToInt32) 'Let's lock the window so it doesn't scroll all crazy.
If All = True Then 'Highlight everything.
For Each pass In frmColors.lbRegExps.Items
RegExp = System.Text.RegularExpressions.Regex.Matches(LCase(rtbMain.Text), LCase(pass))
For Each RegExpMatch In RegExp
rtbMain.Select(RegExpMatch.Index, RegExpMatch.Length)
rtbMain.SelectionColor = ColorTranslator.FromHtml(frmColors.lbHexColors.Items(PassNumber))
Next
PassNumber += 1
Next
Else 'Highlight just that row.
For Each pass In FrmColors.lbRegExps.Items
RegExp = System.Text.RegularExpressions.Regex.Matches(LCase(CurrentLineText), LCase(pass))
For Each RegExpMatch In RegExp
rtbMain.Select(RegExpMatch.Index + (CharsToCurrentLine - RegExpMatch.Length), RegExpMatch.Length)
rtbMain.SelectionColor = Color.Blue
Next
Next
End If
rtbMain.Select(CharsToCurrentLine, 0) 'Reset colors and positon and then unlock drawing.
rtbMain.SelectionColor = Color.Black
LockWindowUpdate(0)
End Sub
你是什麼 「這樣會很奇怪」 是什麼意思? – EndangeredMassa 2008-11-21 15:08:11