Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want RichtextBox as a C# editor so I can write code inside the RichTextBox same like write code in Visual Studio C# and validate code.

What I have tried:

I tried this but it take long time to write.


string pattern = "";
     string[] keywords =
     {
         "bool","void","return","function","string","object",
     };



     foreach (var a in keywords)
     {
         pattern += a + "|";
     }



     Regex R = new Regex(pattern);
     int index = rtbExpression.SelectionStart;



     foreach (Match m in R.Matches(rtbExpression.Text))
     {
         rtbExpression.Select(m.Index, m.Value.Length);
         rtbExpression.SelectionColor = Color.Blue;
         rtbExpression.SelectionStart = index;
     }



     rtbExpression.SelectionColor = Color.Black;



     rtbExpression.ShortcutsEnabled = true;
Posted
Updated 26-Dec-19 5:50am
Comments
[no name] 26-Dec-19 12:05pm    
Check this in case WPF is an option for you: AvalonEdit by icsharpcode[^]

Um. This is going to sound harsh, but ... drop the idea. It's not simple, not at all.
Start here: Syntax Highlighting[^] and that will give you a starting point - note that yioun won't find exactly what you are looking for there, actually validating it will take a lot more work - a whole load more - and isn't a task I would start expecting quick results even with my 40+ years in development behind me. I'd be thinking of maybe a man-year of work, maybe more if I wanted something that kept up-to-date with C# as it evolves - and I would!
You aren't going to get this to work with just a regex, trust me!
 
Share this answer
 
The RichTextBox is a real hassle when trying to implement something like this.
Take a look at: Fast Colored TextBox for Syntax Highlighting[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900