The*_*nja 2 vb.net syntax-highlighting
我正在编写一个简单的代码编辑器,用于我们在工作中使用的非常简单的脚本语言.我的语法突出显示代码工作正常,如果我在整个RichTextBox(rtbMain),但当我试图让它只在那一行,所以我可以运行rtbMain更改的功能,它变得奇怪.我似乎无法弄清楚为什么.我是否正确地走这条路?
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
Run Code Online (Sandbox Code Playgroud)
The*_*nja 11
好吧我明白了.我在rtbMain.TextChange上调用偶数,认为这只会在文本实际发生变化时触发.Nay Nay,如果格式化改变,它也会触发.因此,每当它在第一次通过并突出显示所有内容时改变了某些内容,它就会触发突出显示该行.它会这样做,直到没有什么可以改变.
我为天气设置了一个布尔变量,它当前正在突出显示,并在TextChange子区域中添加了一个if条件
PS我没有自学者徽章,所以欢迎任何升级评分:P