// example from: http://www.codestructs.com/2011/11/syntax-highlighting-in-richtextbox_23.html private void RichTextBox1_TextChanged(object sender, EventArgs e) { int iSelStart = RichTextBox1.SelectionStart; int iSelLength = RichTextBox1.SelectionLength; string tokens = "(auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while)"; Regex rex = new Regex(tokens); MatchCollection mc = rex.Matches(RichTextBox1.Text); int iStart = RichTextBox1.SelectionStart; foreach (Match m in mc) { int iStartIndex = m.Index; int iStopIndex = m.Length; RichTextBox1.Select(iStartIndex, iStopIndex); RichTextBox1.SelectionColor = Color.Blue; RichTextBox1.SelectionStart = iStart; RichTextBox1.SelectionColor = Color.Black; } RichTextBox1.SelectionStart = iSelStart; RichTextBox1.SelectionLength = iSelLength; }
Weiterführende Beispiele: