Post: [C#] Syntax Highlighting
10-14-2011, 04:47 PM #1
tylerallmighty
Human After All
(adsbygoogle = window.adsbygoogle || []).push({});
Syntax Highlighting



Well, as you may have seen I've implemented Syntax Highlighting into my latest CoD program. You're also probably wondering how in the hell I did it.
It was actually pretty easy. I just used Regex (Regular Expressions) to color co-ordinate the keywords.


Step 1:
Well, open you're project that you want to use this method with. If you have a textbox, you're going to have to replace it with a richTextBox, to get the desired output.


Step 2:
Drag a richTextBox, and Timer onto you're form.
You must login or register to view this content.


Double click you're Timer, and add the following code:
You must login or register to view this content.
    
//Get the last cursor position in the richTextBox1.
int selPos = richTextBox1.SelectionStart;


//For each match from the Regex, highlight the word.
foreach (Match keyWordMatch in varNameHere.Matches(richTextBox1.Text))
{
richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
//The color of the highlighted text is Blue.
richTextBox1.SelectionColor = Color.Blue;
//This just keeps the program from highlighting the words after the one you want, from being highlighted.
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionColor = Color.Black;
}



While still in the code viewer, scroll up until you see this:
You must login or register to view this content.
    
public partial class mainForm : Form
{



NOTE: There isn't a closing bracket so, don't try and add one.


Then add this in:
    
public Regex varNameHere = new Regex("words|to|be|highlighted|in|this|color");



"varNameHere" can be anything at all.



That's it! Hope you enjoy!
10-14-2011, 05:47 PM #2
Epic?
Awe-Inspiring
Originally posted by bananaman
Syntax Highlighting



Well, as you may have seen I've implemented Syntax Highlighting into my latest CoD program. You're also probably wondering how in the hell I did it.
It was actually pretty easy. I just used Regex (Regular Expressions) to color co-ordinate the keywords.


Step 1:
Well, open you're project that you want to use this method with. If you have a textbox, you're going to have to replace it with a richTextBox, to get the desired output.


Step 2:
Drag a richTextBox, and Timer onto you're form.
You must login or register to view this content.


Double click you're Timer, and add the following code:
You must login or register to view this content.
    
//Get the last cursor position in the richTextBox1.
int selPos = richTextBox1.SelectionStart;


//For each match from the Regex, highlight the word.
foreach (Match keyWordMatch in varNameHere.Matches(richTextBox1.Text))
{
richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
//The color of the highlighted text is Blue.
richTextBox1.SelectionColor = Color.Blue;
//This just keeps the program from highlighting the words after the one you want, from being highlighted.
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionColor = Color.Black;
}



While still in the code viewer, scroll up until you see this:
You must login or register to view this content.
    
public partial class mainForm : Form
{



NOTE: There isn't a closing bracket so, don't try and add one.


Then add this in:
    
public Regex varNameHere = new Regex("words|to|be|highlighted|in|this|color");



"varNameHere" can be anything at all.



That's it! Hope you enjoy!



Still using the less efficient timer method? Remember what I posted like a few weeks ago on TTR?

You must login or register to view this content. .... That way it only runs Regex when you need it to, plus the Regex itself is more accurate, plus it prevents the screen from flashing each time it updates.
10-14-2011, 06:07 PM #3
tylerallmighty
Human After All
Originally posted by Epic
Still using the less efficient timer method? Remember what I posted like a few weeks ago on TTR?

You must login or register to view this content. .... That way it only runs Regex when you need it to, plus the Regex itself is more accurate, plus it prevents the screen from flashing each time it updates.


I remember. I just wanted to post the one I had created, so you can post your's.
10-14-2011, 09:17 PM #4
Epic?
Awe-Inspiring
Originally posted by bananaman
I remember. I just wanted to post the one I had created, so you can post your's.


Did you code yours by yourself?
10-14-2011, 09:58 PM #5
tylerallmighty
Human After All
Originally posted by Epic
Did you code yours by yourself?


Yup.
10 char.

Copyright © 2025, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo