Skip to content

Instantly share code, notes, and snippets.

@guillaC
Created August 29, 2020 15:27
Show Gist options
  • Save guillaC/c5d551cc3f71c18039c56bf19bfd4a77 to your computer and use it in GitHub Desktop.
Save guillaC/c5d551cc3f71c18039c56bf19bfd4a77 to your computer and use it in GitHub Desktop.
RichTextBox Syntax Highlighting
private void Highligh(RichTextBox rtb) {
String[] keywords = {
"@echo",
"echo",
"pause",
"set",
"cls",
"exit",
"del",
"erase",
"exit"
};
String[] keywordsControl = {
"if",
"for",
"goto",
"call"
};
string[] code = rtb.Lines;
rtb.Clear();
for (int i = 0; i < code.Length; i++) {
if (code.Length - 1 != i) code[i] += System.Environment.NewLine; //ajout du retour chariot dans la chaine de caracteres
String[] words = new Regex("([^\\s]+)").Split(code[i]);
foreach(string word in words) {
if (word.Length > 1) //aucune vérification sur les espaces
{
if (keywords.Contains(word.ToLower())) { //coloration des commandes
rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily, rtb.SelectionFont.Size, FontStyle.Bold);
rtb.SelectionColor = System.Drawing.ColorTranslator.FromHtml("#D56AA0"); //Rouge
}
else if (keywordsControl.Contains(word.ToLower())) { //coloration des structures de controle
rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily, rtb.SelectionFont.Size, FontStyle.Bold);
rtb.SelectionColor = System.Drawing.ColorTranslator.FromHtml("#6BBABF"); //Cyan
}
else if (word.Substring(0, 1) == word.Substring(word.Length - 1, 1) && word.Substring(0, 1) == "%") { //coloration des variables
rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily, rtb.SelectionFont.Size, FontStyle.Bold);
rtb.SelectionColor = System.Drawing.ColorTranslator.FromHtml("#F1BA59"); //Orange
}
}
rtb.SelectedText = word;
rtb.SelectionColor = System.Drawing.ColorTranslator.FromHtml("#F2F5F2");
rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily, rtb.SelectionFont.Size, FontStyle.Regular); //Blanc
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment