2016-05-02 4 views
3

私自身のプログラミング言語用のテキストエディタの作成に取り組んでいます。それはうまく動作しますが、私には1つの問題があります。私はこのような何か入力すると:forループ角括弧の強調表示が正しく機能しません - VB.NET

for (int i = 0, 10; i++) { 
    console.println(i); 
} <== This one 

void Example() { 
    for (int i = 0, 10; i++) { 
    console.println(i); 
    } 
} 

閉じ括弧を強調表示されません。誰でも助けてくれますか? ありがとうございます!ここで

は私のコードです:

Imports System.Drawing.Color 
Imports System.Drawing.Font 

Public Class Form1 

Private Sub Highlight(ByRef Text As String(), ByRef Name As String) 
    Dim Color As Color = Nothing 
    Dim Font As Font = Nothing 
    Select Case Name 
     Case "Keywords" 
      Color = Blue 
      Font = NewFont(FontStyle.Regular) 
     Case "Functions" 
      Color = Black 
      Font = NewFont(FontStyle.Italic) 
     Case "Classes" 
      Color = Cyan 
      Font = NewFont(FontStyle.Regular) 
     Case "Types" 
      Color = Purple 
      Font = NewFont(FontStyle.Regular) 
     Case "Operators" 
      Color = GreenYellow 
      Font = NewFont(FontStyle.Regular) 
     Case "Brackets" 
      Color = Red 
      Font = NewFont(FontStyle.Regular) 
    End Select 
    Dim CursorPos As Integer = tb.SelectionStart 
    For i As Integer = 0 To Text.Length - 1 
     FindAll(tb, Text(i)) 
     tb.SelectionColor = Color 
     tb.SelectionFont = Font 
     tb.DeselectAll() 
    Next 
    tb.SelectionStart = CursorPos 
    tb.SelectionColor = Nothing 
    tb.SelectionFont = NewFont(FontStyle.Regular) 
End Sub 

Private Function NewFont(ByVal Style As FontStyle) 
    Return New Font(tb.Font, Style) 
End Function 

Private Sub FindAll(ByRef tb As RichTextBox, ByRef Find As String) 
    Dim StartIndex As Integer = 0 
    Dim Text As String = tb.Text 
    Do 
     Dim Index As Integer = Text.IndexOf(Find, StartIndex) 
     If Index < 0 Then 
      Exit Do 
     End If 
     tb.Select(Index, Find.Length) 
     StartIndex = Index + 1 
    Loop 
End Sub 

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb.TextChanged 
    Dim Keywords As String() = {"new", "using", "void", "function", "public", "protected", "private", "if", "else", "for", "loop", "while", "until", "true", "false", "null", "default"} 
    Dim Types As String() = {"string", "int", "long", "byte", "char"} 
    Dim Brackets As String() = {"(", ")", "[", "]", "{", "}"} 
    Dim Operators As String() = {"+", "-", "=", "/", "*"} 
    Dim Classes As String() = {"console", "color", "font"} 
    Dim Functions As String() = {"print", "println"} 
    Highlight(Keywords, "Keywords") 
    Highlight(Types, "Types") 
    Highlight(Brackets, "Brackets") 
    Highlight(Operators, "Operators") 
    Highlight(Classes, "Classes") 
    Highlight(Functions, "Functions") 
End Sub 
End Class 
+1

 Dim CursorPos As Integer = tb.SelectionStart For i As Integer = 0 To Text.Length - 1 Dim StartIndex As Integer = 0 Do FindAll(tb, Text(i), StartIndex) If StartIndex = 0 Then Exit Do End If tb.SelectionColor = Color tb.SelectionFont = Font tb.DeselectAll() Loop tb.SelectionStart = CursorPos tb.SelectionColor = Nothing tb.SelectionFont = NewFont(FontStyle.Regular) Next 
あなたは 'TextChanged'イベントのうちのアレイを動かすべきの終了時にループについて(または少なくともStatic''にそれらを設定します)。毎回新しいものを宣言することは記憶の浪費です。 –

+0

ありがとうございますが、動作しません。ブラケットはまだハイライトされていません。 – HighTechProgramming15

+0

私はそれが解決策ではないと言いました。しかし、メモリの使用量が減少し、速度が少し向上する可能性があります。 –

答えて

0

あなたのコードは唯一成功した各検索の後、フォントや色を適用することにより...など、

をオペレータが各ブラケットの最後の発生をhighlitingされる影響しますテキストは正しく。

はところで、あなたのコードがun-HILITEないキーワードを行います(例えばのボイドは、IDがハイライト化したままVOに変更)

アウトカム tadaaは

enter image description here

を変更FindAll()手順

Private Sub FindAll(ByRef tb As RichTextBox, ByRef Find As String, ByRef StartIndex As Integer) 

    Dim Text As String = tb.Text 

    Dim Index As Integer = Text.IndexOf(Find, StartIndex) 
    StartIndex = Index + 1 
    If Index < 0 Then 
     Exit Sub 
    End If 
    tb.Select(Index, Find.Length) 
End Sub 

ハイライト手順

+0

ありがとう!出来た!しかし、** vo id **はまだ強調表示されています。 – HighTechProgramming15

+0

@ HighTechProgramming15:それを行ってください。あなたの罰金 –

関連する問題