2

我正在为我们在工作中使用的一种非常简单的脚本语言编写一个简单的代码编辑器。如果我在整个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
4

3 回答 3

11

好的,我想通了。我在 rtbMain.TextChange 上调用了偶数,认为只有在文本实际更改时才会触发。不,不,如果格式更改,它也会触发。因此,每次它在第一次通过并突出显示所有内容时更改某些内容,然后它会触发突出显示该行。它会这样做,直到没有任何东西可以改变。

我为天气设置了一个布尔变量,它当前是否突出显示,并在 TextChange 子中添加了一个 if 条件

PS我没有自学者徽章,所以欢迎任何提高评级:P

于 2008-11-21T23:07:03.250 回答
2

这并不能真正回答您的问题,但如果您正在编写自己的编辑器,则最好使用为 .NET 完成的一些现有开源工作。我建议:

Roger Alsing 的SyntaxBox

于 2008-11-21T23:44:13.130 回答
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
于 2009-04-22T11:35:10.560 回答