2008-11-21 7 views
2

Je suis en train de coder un simple éditeur de code pour un langage de script très simple que nous utilisons au travail. Mon code de coloration syntaxique fonctionne très bien si je le fais sur l'ensemble RichTextBox (rtbMain) mais quand j'essaye de le faire fonctionner sur cette ligne, donc je peux exécuter la fonction avec les changements rtbMain, ça devient bizarre. Je n'arrive pas à comprendre pourquoi. Est-ce que j'y vais même de la bonne façon?Mise en surbrillance de la syntaxe VB.NET dans une zone de texte enrichi

rtbMain est la zone de texte principale. frmColors.lbRegExps est une liste de mots à mettre en évidence (plus tard, il aura des expressions régulières légèrement plus puissantes.) frmColor.lbHexColors est une autre zone de liste avec les couleurs hexadécimales correspondantes pour les mots.

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

Que voulez-vous dire par "ça devient bizarre"? – EndangeredMassa

+0

Avez-vous déjà utilisé Notepad ++? Il est vraiment facile de créer une coloration syntaxique pour une langue définie par l'utilisateur et ainsi vous n'auriez même pas besoin d'écrire ceci. Bien sûr, vous pourriez avoir d'autres raisons, mais si vous créez simplement un éditeur autonome, c'est quelque chose à considérer. –

+0

N'importe quel code peut devenir bizarre s'il est précédé de "Sur erreur Resume Suivant " .... = P. Mais sérieusement, que fait-il exactement quand il essaie d'analyser une seule ligne? –

Répondre

11

Ok, je l'ai compris. J'appelais le même sur rtbMain.TextChange en pensant que cela ne se déclencherait que si le texte changeait réellement. Nay Nay, il se déclenchera également si le formatage est modifié. donc à chaque fois qu'il changeait quelque chose pendant qu'il faisait sa première passe et qu'il mettait tout en évidence, il se déclenchait alors pour mettre en évidence la ligne. Il le ferait jusqu'à ce qu'il n'y ait plus rien à changer.

I définir une variable booléenne pour le temps ou non été mise en évidence et ajouté actuellement si l'état intérieur du TextChange sous

post-scriptum Je n'ai pas le badge d'auto-apprenant, donc les notes seraient les bienvenues: P

2

Cela ne répond pas vraiment à votre question, mais si vous écrivez votre propre éditeur, vous feriez peut-être mieux d'utiliser certains travail open source qui a été fait pour .NET. Je vous recommande:

Roger Alsing de 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 
Questions connexes