我正在编写一个简单的代码编辑器,用于我们在工作中使用的非常简单的脚本语言。如果我在整个RichTextBox
(rtbMain
)上执行此语法,突出显示代码的语法很好,但是当我尝试让它在该行上工作时,我可以使用{{1}运行该函数变化,变得奇怪。我似乎无法弄清楚为什么。我是否正确地采取了这种方式?
rtbMain
是主要文本框。
rtbMain
是要突出显示的单词列表框(稍后它会有更强大的正则表达式。)
frmColors.lbRegExps
是另一个列表框,其中包含相应的十六进制颜色。
frmColor.lbHexColors
答案 0 :(得分:11)
好的我明白了。我在rtbMain.TextChange上调用偶数,认为这只会在文本实际发生变化时触发。 Nay Nay,如果格式化改变,它也会触发。因此,每当它在第一次通过并突出显示所有内容时改变了某些内容,它就会触发突出显示该行。它会这样做,直到没有什么可以改变。
我为天气设置了一个布尔变量,它当前是否突出显示并在TextChange子句中添加了一个if条件
P.S。 我没有自学者徽章,所以欢迎任何升级评分:P
答案 1 :(得分:2)
这并没有真正回答你的问题,但是如果你正在编写自己的编辑器,那么最好使用一些已经为.NET完成的现有开源工作。我建议:
Roger Alsing的SyntaxBox
答案 2 :(得分:0)
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) ''lets lock the window so it doesnt 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 ''higlight 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