2008-11-21 113 views
2

我正在爲我們在工作中使用的非常簡單的腳本語言編寫簡單的代碼編輯器。我的語法突出顯示代碼工作正常,如果我在整個RichTextBoxrtbMain)上執行它,但是當我試圖讓它在該行上工作時,所以我可以使用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 
+0

你是什麼 「這樣會很奇怪」 是什麼意思? – EndangeredMassa 2008-11-21 15:08:11

回答

11

好的我想通了。我甚至在rtbMain.TextChange上打電話,認爲這隻會在文本實際發生改變時觸發。否則,如果格式更改,它也會觸發。所以每次它在第一次傳球時改變了一些東西並突出顯示了所有內容,它就會觸發突出顯示該線。它會這樣做,直到沒有什麼可以改變。

我設置爲天氣一個布爾變量或不它目前突出顯示,並增加了如果TextChange子

P.S.內部條件 我沒有自學者徽章,所以任何提高收視率將是受歡迎的:P

2

這並不真正回答你的問題,但如果你正在編寫自己的編輯器,你可能會更好使用一些現有的已經爲.NET完成的開源工作。我建議:

羅傑Alsing的SyntaxBox

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