I wanted to add Syntax Highlighting to my notepad, I am using Duncan Harris' code to do this. I have two files right now, Main.cs - The main Form class and Utilities.cs - I have moved all of the Syntax highlighting functions into this class.
So, my question is: Why doesn't this code work?
Main.cs
private void Parse() { Util.Parse(); }
private void ParseLine(string line) { Util.ParseLine(line); }
private void TextChangedEvent(object sender, EventArgs e) { Util.TextChangedEvent(null, null); }
Utilities.cs
private
Form _form; private TabControl _tabs;public
void Controller(Form form, TabControl tabs){
_form = form;
_tabs = tabs;
}
public
void Parse(){
RichTextBox
rtb = (RichTextBox)_tabs.SelectedTab.Controls[0];/* THIS IS THE PROBLEM LINE */
rtb.TextChanged +=
new EventHandler(this.TextChangedEvent);}
{
RichTextBox rtb = (RichTextBox)_tabs.SelectedTab.Controls[0]; // Calculate the starting position of the current line. int start = 0, end = 0; for (start = rtb.SelectionStart - 1; start > 0; start--){
if (rtb.Text[start] == '\n') { start++; break; }}
if (start < 0) return; // Calculate the end position of the current line. for (end = rtb.SelectionStart; end < rtb.Text.Length; end++){
if (rtb.Text[end] == '\n') break;}
// Extract the current line that is being edited. String line = rtb.Text.Substring(start, end - start); // Backup the users current selection point. int selectionStart = rtb.SelectionStart; int selectionLength = rtb.SelectionLength; // Split the line into tokens. Regex r = new Regex("([ \\t{}();])"); string[] tokens = r.Split(line); int index = start; foreach (string token in tokens){
// Set the token's default color and font.rtb.SelectionStart = index;
rtb.SelectionLength = token.Length;
rtb.SelectionColor =
Color.Black;rtb.SelectionFont =
new Font("Courier New", 10, FontStyle.Regular); // Check for a comment. if (token == "//" || token.StartsWith("//")){
// Find the start of the comment and then extract the whole comment. int length = line.Length - (index - start); string commentText = rtb.Text.Substring(index, length);rtb.SelectionStart = index;
rtb.SelectionLength = length;
rtb.SelectionColor =
Color.LightGreen;rtb.SelectionFont =
new Font("Courier New", 10, FontStyle.Regular); break;}
// Check whether the token is a keyword. String[] keywords = { "public", "void", "using", "static", "class", "int", "if", "for", "each", "string" }; for (int i = 0; i < keywords.Length; i++){
if (keywords[i] == token){
// Apply alternative color and font to highlight keyword.rtb.SelectionColor =
Color.Blue;rtb.SelectionFont =
new Font("Courier New", 10, FontStyle.Bold); break;}
}
index += token.Length;
}
// Restore the users current selection point.rtb.SelectionStart = selectionStart;
rtb.SelectionLength = selectionLength;
}
Again, a lot of this code is not mine, but Duncan Harris' This code works when not moved into a new class. Sorry for the large amount of code, it is just the problem area.
Thanks for any help you can give.
Replies
Know the answer? Post it — somebody with the same question will find it here.