Ryan Turney

Hello everyone, I'm pretty new to C# so for my first project I deceided to create a simple Notepad clone, except it will be much more powerful including tabbed files. It has been pretty easy until now...

I wanted to add Syntax Highlighting to my notepad, I am using Duncan Harris' code from http://www.c-sharpcorner.com/ 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


Code Snippet
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
); }
Code Snippet
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);

}


public void TextChangedEvent(object sender, EventArgs e)

{

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' from http://www.c-sharpcorner.com/ 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.


Re: Windows Forms General EventHandler Problem

Perumal.R

Hi,

Its working fine.

You just have to add the following code in your main Form class.

Code Snippet

Utilities myClass = new Utilities();

private void Form1_Load(object sender, EventArgs e)

{

myClass.Controller(this, tabControl1);

myClass.Parse();

}

Regards,

Perumal.R





Re: Windows Forms General EventHandler Problem

Soe Moe

hi,

could you tell me about these questions.

where you get the error.

compile error or runtime error.

what is error message.

soemoe






Re: Windows Forms General EventHandler Problem

Perumal.R

hi,

I did not get any errors.

Can you please be a bit clear about how did you call the method from your main form class

(Provide the exact code).

Regards,

Perumal.R





Re: Windows Forms General EventHandler Problem

Pink Splat

I'm sorry, here is all the relevant code in Main.cs and everything in Utilities.cs

There are no errors, no warnings, no nothing. It just doesn't work. Ex: The world "class" should be highlighted blue, but nothing happens when "class" is typed.

And sorry Perumal.R, that did not help me.

Code Snippet

Main.cs

public partial class Main : Form

{

Utilities Util = new Utilities();

public RichTextBox textBox = new RichTextBox();

string line;

public Main()

{

InitializeComponent();

Fmenu.Controller(this, tabs);

Emenu.Controller(this, tabs);

Hmenu.Controller(this, tabs);

Util.Controller(this, tabs);

}

/*******************************************************************

* Utilities *

* *****************************************************************/

private void Parse() { Util.Parse(); }

private void ParseLine(string line) { Util.ParseLine(line); }

private void TextChangedEvent() { Util.TextChangedEvent(null, null); }

}

Code Snippet

Utilities.cs

namespace Simple_Pad

{

class Utilities

{

private Form _form;

private TabControl _tabs;

private string _line;

public void Controller(Form form, TabControl tabs)

{

_form = form;

_tabs = tabs;

}

public void Parse()

{

RichTextBox rtb = new RichTextBox();

/* THIS IS WHERE I THINK IS HAS PROBLEMS */

rtb.TextChanged += new EventHandler(TextChangedEvent);

}

public void ParseLine(string line)

{

RichTextBox rtb = (RichTextBox)_tabs.SelectedTab.Controls[0];

Regex r = new Regex("([ \\t{}();])");

String[] tokens = r.Split(line);

_line = line;

foreach (string token in tokens)

{

// Set the token's default color and font.

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 index = _line.IndexOf("//");

string comment = _line.Substring(index, _line.Length - index);

rtb.SelectionColor = Color.DarkGreen;

rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);

rtb.SelectedText = comment;

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;

}

}

rtb.SelectedText = token;

}

rtb.SelectedText = "\n";

}

public void TextChangedEvent(object sender, EventArgs e)

{

// 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;

}

}

Hopefully this is enough code to help me solve my problem. If more is needed please ask, but there really is nothing more I can show that relates to this problem.

Thanks






Re: Windows Forms General EventHandler Problem

Perumal.R

Hi,

I think you havent attached the event handler to the control's event. i.e. you haven't called the Parse method in the constructor.

Do the following:

Code Snippet

public Main()

{

InitializeComponent();

Fmenu.Controller(this, tabs);

Emenu.Controller(this, tabs);

Hmenu.Controller(this, tabs);

Util.Controller(this, tabs);

Util.Parse();

}

Call the Util.Parse() method in the constructor. Now the event handler adds the

TextChanged method to the RichTextBox's TextChanged event.

I hope this could help your course.

Regards,

Perumal.R





Re: Windows Forms General EventHandler Problem

Pink Splat

I'm sorry, that does not solve the problem. When I do that it still compiles cleanly, and runs like it should besides the fact that it won't highlight the keywords.

EDIT: Thank you guys! I got it to work, I defined the RichTextBox wrong in one place and that threw everything off, Perumal.R your idea above allowed me to fix the other problems, thank you.






Re: Windows Forms General EventHandler Problem

- Jason -

I have the same problem, what did you define incorrectly Here is a sample of my code with the same issue.

itemid = System.Guid.NewGuid().ToString();

tb = new TextBox();

tb.ID = "item" + itemid;

tb.MaxLength = 4;

tb.Text = "0";

tb.TextChanged += new System.EventHandler(TextChanged);

tb.AutoPostBack = true;

private void TextChanged(object sender, EventArgs e)

{

System.Web.UI.WebControls.TextBox tb = sender as TextBox;

}





Re: Windows Forms General EventHandler Problem

Pink Splat

I defined the RichTextBox wrong, it define a RichTextBox, but it didn't have it selected as the first control on my tabbed pages.

Code Snippet

public void Parse()

{

RichTextBox rtb = new RichTextBox();

/* THIS IS WHERE I THINK IS HAS PROBLEMS */

rtb.TextChanged += new EventHandler(TextChangedEvent);

}

Should have been:

Code Snippet

public void Parse()

{

RichTextBox rtb = (RichTextBox)_tabs.SelectedTab.Controls[0];

/* THIS IS WHERE I THINK IS HAS PROBLEMS */

rtb.TextChanged += new EventHandler(TextChangedEvent);

}

Everything was working fine, just the textbox was defined wrong and that threw me off.