2011-08-03 13 views
1

ロジックプログラム用のRichTextBoxを使用して簡単なvb.netテキストエディタを作成しました。コメントを除いて作業するために色付け(強調表示)しています。しかし、それは本当に本当に遅く100行程度の後に実行されます。誰かがこれを行うより効率的な方法を知っていますか? 注:RichTextBox TextChangedイベントでSyntaxHandlerを呼び出します。RichTextBoxを使用するVB.Netエディタ

Friend vbKeys As String = "And|As|Case|Catch|CDbl|Ceiling|CInt|Class|Const|Continue|CStr|Decimal|" & _ 
          "Default|Delegate|Dim|Do|Double|Each|End|Else|Enum|Event|" & _ 
          "Explicit|Extern|False|Finally|Floor|For|Format|GoTo|If|IIf|In|Int|Is|Long|Module|" & _ 
          "Namespace|New|Next|Not|Null|Object|Option|Or|Override|Params|PI|Private|Protected|" & _ 
          "Public|Readonly|Ref|Replace|Return|Round|Sbyte|Sealed|Select|Short|Sqrt|" & _ 
          "Static|String|Structure|Sub|Then|Throw|True|Try|TypeOf|Uint|Ulong|" & _ 
          "Unchecked|Using|With|While" 

Friend Sub SyntaxHandler(ByVal txtScript As RichTextBox) 
    Dim selPos As Integer = txtScript.SelectionStart 

    'set everything to black to start with 
    txtScript.SelectAll() 
    txtScript.SelectionColor = Color.Black 

    'Regex Variables for user 
    FormatWithRegEx("\b(?:" & regexVaribles & ")\b", txtScript, Color.DarkViolet) 

    'double quoted strings are all red 
    FormatWithRegEx("""", txtScript, Color.Red) 
    FormatWithRegEx("""[^""]*""", txtScript, Color.Red) 

    'reserved words are all blue 
    FormatWithRegEx("\b(?:" & vbKeys & ")\b", txtScript, Color.Blue) 

    'single line comments are all green 
    FormatWithRegEx("'[\w*\t*\S*\[ ]*]*", txtScript, Color.Green) 

    txtScript.Select(selPos, 0) 
    txtScript.SelectionColor = Color.Black 

End Sub 
Private Sub FormatWithRegEx(ByVal strRegEx As String, ByRef txtRTB As RichTextBox,  ByVal colour As System.Drawing.Color) 
    Dim regex As New Regex(strRegEx, _ 
    RegexOptions.IgnoreCase _ 
    Or RegexOptions.Multiline _ 
    Or RegexOptions.Singleline _ 
    Or RegexOptions.IgnorePatternWhitespace) 

    Dim myMatches As MatchCollection = regex.Matches(txtRTB.Text) 
    For Each GoodMatch As Match In myMatches 
     txtRTB.Select(GoodMatch.Index, GoodMatch.Length) 
     txtRTB.SelectionColor = colour 
    Next 
End Sub 
+0

私はそれを速くする方法はわかりませんが、キーを押すたびにコントロール内のすべてのテキストに対して5つの正規表現を実行しているため、おそらく遅いです。実際にテキストを実際に解析する方が効率的です(しかしより困難です)。 – Andy

+0

マイクロソフトはどうしているのだろうか? – TroyS

+0

@Andyはい、おそらくあなたの権利。私はそのショットを与えるでしょう。 RegExよりも効率的な解決のための提案やコードスニペットがある場合は、お気軽に投稿してください。ありがとうございました。 – TroyS

答えて

1

一つの選択肢(例えば、スタックオーバーフローによって使用され、飾り立てる)既存のライブラリを使用することです。

関連する問題