I want to colorize comments shown in RTB in the three formats :
1-2-PHP Code:/*
*This is a comment
*/
PHP Code://This is comment
//
3-I want only the regex pattern . Thanks guys .PHP Code:///This is function description
Printable View
I want to colorize comments shown in RTB in the three formats :
1-2-PHP Code:/*
*This is a comment
*/
PHP Code://This is comment
//
3-I want only the regex pattern . Thanks guys .PHP Code:///This is function description
check this link pirate mate , it's perl ( Syntax highlighting C# code ) , but it's not to hard to follow ...
Using Perl to Syntax Color C#
Thanks , I'll check it out and see if I can do something with it . :)
I couldn't figure out the pattern for comment in that link.:(
*bump*
ok here's a quick example i knocked up for the /* comment */ type of comments @"[/*]\s*([^"">]+)\s*[*/]\s*[/]" being the pattern ...
for the // comments it's @"[//]\s*([^"">]+)\s*[\n]" ...VB Code:
[color=blue]private void[/color] richTextBox1_TextChanged([color=blue]object[/color] sender, System.EventArgs e) { Regex rgx=[color=blue]new[/color] Regex(@"[/*]s*([^"">]+)s*[*/]s*[/]" , RegexOptions.IgnoreCase); MatchCollection mCol=rgx.Matches(richTextBox1.Text); [color=blue]foreach[/color](Match m [color=blue]in[/color] mCol) { richTextBox1.HideSelection=[color=blue]true[/color]; richTextBox1.Select(m.Index , m.Length); richTextBox1.SelectionColor=Color.Green; richTextBox1.SelectionStart=m.Index + m.Length; richTextBox1.SelectionLength=0; } }
hope it helps mate :wave:VB Code:
[color=blue]private void[/color] richTextBox1_TextChanged([color=blue]object[/color] sender, System.EventArgs e) { Regex rgx=[color=blue]new[/color] Regex(@"[//]s*([^"">]+)s*[\n]" , RegexOptions.IgnoreCase); MatchCollection mCol=rgx.Matches(richTextBox1.Text); [color=blue]foreach[/color](Match m [color=blue]in[/color] mCol) { richTextBox1.HideSelection=[color=blue]true[/color]; richTextBox1.Select(m.Index , m.Length); richTextBox1.SelectionColor=Color.Green; richTextBox1.SelectionStart=m.Index + m.Length; richTextBox1.SelectionLength=0; } }
That did the trick 80% correct except this :
line of code here //this is comment
another code here
the second line "another code here" is considered to be comment also but actually it's code .
1-How can I fix this ?
2-It skip these "#region" and this " /// <summary>....<summary/>.
Thanks so far , dude .
you need to set the selectioncolor back to it's default ( eg: Color.Black ) after highlighting , also it's better under a button click or a void rather than the text changed area.
PHP Code:private void button1_Click(object sender, System.EventArgs e)
{
Regex rgx=new Regex(@"[//]\s*([^"">]+)\s*[\n]" , RegexOptions.IgnoreCase);
MatchCollection mCol=rgx.Matches(richTextBox1.Text);
foreach(Match m in mCol)
{
richTextBox1.HideSelection=true;
richTextBox1.Select(m.Index , m.Length);
richTextBox1.SelectionColor=Color.Green;
richTextBox1.SelectionStart=m.Index + m.Length;
richTextBox1.SelectionLength=0;
richTextBox1.SelectionColor=Color.Black; // revert the text back to it's default color.
}
}
I'll check this when I get home....:D
Thanks a lot , that seems working now . One more
2-It skip these "#region" and this " /// <summary>....<summary/>.
??
hello mate. i just make something similar but won't work with
this is what i've gotPHP Code:/* this is a comment
*/
still missing C style comment [multiline]. any ideas?PHP Code:private void button1_Click(object sender, System.EventArgs e)
{
Regex rgx=new Regex(@"(//.*[\n])|(///.*[\n])|(#region.*[\n])|(/\*.*?\*/)" ,
RegexOptions.IgnoreCase);
MatchCollection mCol=rgx.Matches(richTextBox1.Text);
foreach(Match m in mCol)
{
richTextBox1.HideSelection=true;
richTextBox1.Select(m.Index , m.Length);
richTextBox1.SelectionColor=Color.Green;
richTextBox1.SelectionStart=m.Index + m.Length;
richTextBox1.SelectionLength=0;
richTextBox1.SelectionColor=Color.Black; // revert the text back to it's default color.
}
}
halu mate. got it. but still i don't know if this efficient. lol
Code:Regex rgx=new Regex(@"(//.*[\n])|(///.*[\n])|"+
@"(#region.*[\n])|(/\*.*?\*/)|(/\*[^""]*\*/)" ,
RegexOptions.IgnoreCase);
I tried to include the xor case in the regex pattern and it slowed things incredibly . When I get home I'll try to work it out!Quote:
Originally posted by brown monkey
halu mate. got it. but still i don't if this efficient. lol
PHP Code:Regex rgx=new Regex(@"(//.*[\n])|(///.*[\n])|"+
@"(#region.*[\n])|(/\*.*?\*/)|(/\*[^""]*\*/)" ,
RegexOptions.IgnoreCase);