Richtextbox pour coloration syntaxique en temps reel

5/5 (8 avis)

Vue 10 276 fois - Téléchargée 1 268 fois

Description

Bonjour,

J'ai adapté un de mes projets au boulot pour le rendre réutilisable et facilement modifiable pour d'autres (vous, membres de CS).

Voici donc une classe dérivée d'un RichTextBox, fournissant un coloriage personnalisable à l'aide d'expression rationnelles.

Ici, pour l'exemple, une coloration (partielle?) du langage SQL.

J'utilises le TOM (Text Object Model) pour manipuler des range de caractères.
ca permet un formatage plus fin, pas d'interaction avec la selection de l'utilisateur, pas de scintillement car aucun refresh en cours de route, etc.

Facilement convertible, je pense en VB.Net

Conclusion :


Je vous invite à placer en commentaire de la source les différents jeux de ColorItem que vous emploierez pour afficher tel ou tel langage...

Codes Sources

A voir également

Ajouter un commentaire Commentaires
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 72
22 mars 2011 à 10:34
// SQL

RegexOptions opt = RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.IgnoreCase;
richTextBox1.ColorItems.Add(new ColorItem(@"'.*?'", Color.Red));
richTextBox1.ColorItems.Add(new ColorItem(@"\b\d+\b", Color.HotPink));
richTextBox1.ColorItems.Add(new ColorItem(@"\b\d*\.\d+\b", Color.HotPink));
richTextBox1.ColorItems.Add(new ColorItem(@"\b(" +
@"AND|IS|&&|LOG|NOT|NOW|MIN|!|\|\||OR|OCT|TAN|STD|SHA|ORD|XOR)\b", Color.Orange, opt));
richTextBox1.ColorItems.Add(new ColorItem(@"\b(" +
@"SELECT|UPDATE|INSERT|DELETE|USING|LIMIT|OFFSET|SET)\b", Color.Blue, opt));
richTextBox1.ColorItems.Add(new ColorItem(@"\b(" +
@"DATE|INTO|FROM|THEN|WHEN|WHERE|JOIN|ELSE)\b", Color.Green, opt));
richTextBox1.ColorItems.Add(new ColorItem(@"\b(" +
@"ABS|ACOS|ADDDATE|ADDTIME|AES_DECRYPT|AES_ENCRYPT|ASCII|ASIN|ATAN2|ATAN|AVG|" +
@"BETWEEN|BIN|BINARY|BIT_AND|BIT_LENGTH|BIT_OR|BIT_XOR|" +
@"CASE|CAST|CEIL|CEILING|CHAR_LENGTH|CHAR|CHARACTER_LENGTH|CHARSET|COALESCE|COERCIBILITY" +
@"COLLATION|COMPRESS|CONCAT_WS|CONCAT|CONNECTION_ID|CONV|CONVERT_TZ|Convert|COS|COT|" +
@"COUNT|COUNT|COUNT\(DISTINCT\)|CRC32|CURDATE|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|CURRENT_USER|CURTIME|" +
@"DATABASE|DATE_ADD|DATE_FORMAT|DATE_SUB|DATEDIFF|DAY |DAYNAME|DAYOFMONTH|DAYOFWEEK|DAYOFYEAR|DECODE|DEFAULT|" +
@"DEGREES|DES_DECRYPT|DES_ENCRYPT|DIV|" +
@"ELT|ENCODE|ENCRYPT|<=>|EXP\(\)|EXPORT_SET|EXTRACT|" +
@"FIELD|FIND_IN_SET|FLOOR|FORMAT|FOUND_ROWS|FROM_DAYS|FROM_UNIXTIME|" +
@"GET_FORMAT|GET_LOCK|GREATEST|GROUP_CONCAT|" +
@"HEX|HOUR|" +
@"IF|IFNULL|IN|INET_ATON|INET_NTOA|INSTR|IS_FREE_LOCK|ISNOTNULL|ISNOT|ISNULL|IS_USED_LOCK|ISNULL|" +
@"LAST_DAY|LAST_INSERT_ID|LCASE|LEAST|<<|LEFT|LENGTH|LIKE|LN|LOAD_FILE|LOCALTIME|LOCALTIMESTAMP|LOCATE|LOG10|LOG2|LOWER|LPAD|LTRIM|" +
@"MAKE_SET|MAKEDATE|MAKETIME|MASTER_POS_WAIT|MATCH|MAX|MD5|MICROSECOND|MID|MINUTE|MOD|%|MONTH|MONTHNAME|" +
@"NOTBETWEEN|!=|NOTIN|NOTLIKE|NOTREGEXP|NULLIF|" +
@"OCTET_LENGTH|OLD_PASSWORD|ORD|" +
@"PASSWORD|PERIOD_ADD|PERIOD_DIFF|PI|\+|POSITION|POW|POWER|PROCEDUREANALYSE|" +
@"QUARTER|QUOTE|" +
@"RADIANS|RAND|REGEXP|RELEASE_LOCK|REPEAT|REPLACE|REVERSE|>>|RIGHT|RLIKE|ROUND|ROW_COUN|RPAD|RTRIM|" +
@"SCHEMA|SEC_TO_TIME|SECOND|SESSION_USER|SHA1|SIGN|SLEEP|SOUNDEX|SOUNDSLIKE|SPACE|SQRT|STDDEV_POP|STDDEV_SAMP|" +
@"STDDEV|STR_TO_DATE|SUBDATE|SUBSTR|SUBSTRING_INDEX|SUBSTRING|SUBTIME|SUM|SYSDATE|SYSTEM_USER|" +
@"TIME_FORMAT|TIME_TO_SEC|TIME|TIMEDIFF|\*|TIMESTAMP|TIMESTAMPADD|TIMESTAMPDIFF|TO_DAYS|TRIM|TRUNCATE|" +
@"UCASE|UNCOMPRESS|UNCOMPRESSED_LENGTH|UNHEX|UNIX_TIMESTAMP|UPPER|USER|UTC_DATE|UTC_TIME|UTC_TIMESTAMP|UUID|" +
@"VALUES|VAR_POP|VAR_SAMP|VARIANCE|VERSION|" +
@"WEEK|WEEKDAY|WEEKOFYEAR|" +
@"YEAR|YEARWEE)\b", Color.Brown, opt));
richTextBox1.DefaultColorItem.Forecolor = Color.Black;
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 36
22 mars 2011 à 11:54
Bon boulot :) !
Arthenius Messages postés 1182 Date d'inscription mercredi 21 janvier 2004 Statut Membre Dernière intervention 6 septembre 2011 14
22 mars 2011 à 16:08
>bookmark

merci ca pourrait servir :)
LUDINSKI Messages postés 441 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 22 mai 2012 7
27 mars 2011 à 17:38
Très bonne source...
Je met un 9/10, bien mérité ;)

Bonne continuation !
SonicDusbir Messages postés 30 Date d'inscription dimanche 24 septembre 2006 Statut Membre Dernière intervention 5 juillet 2011
5 juil. 2011 à 22:13
Super la source ! Je vais l'utiliser pour une coloration syntaxique de VB.NET.

Cependant j'aimerais remplacé la richTextBox par une KryptonRichTextBox de la suite Krypton.
Mais une exception est levée lors de l'appel à la fonction Create.

Quelqu'un peut m'aider ? Merci !

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.