winforms - Readonly richtextbox changing color of keyword C# -


i have rich text box using display example of hello world program , want keywords such 'using' 'namespace' 'class' 'static' 'void' 'string' display blue.

i have got 'using' display blue when user starts type or types 'using' want richtextbox read not allowing user input

here code:

    private void rtb_textchanged(object sender, eventargs e)     {         string find = "using";         if (rtb.text.contains(find))         {             var matchstring = regex.escape(find);             foreach (match match in regex.matches(rtb.text, matchstring))             {                 rtb.select(match.index, find.length);                 rtb.selectioncolor = color.blue;                 rtb.select(rtb.textlength, 0);                 rtb.selectioncolor = rtb.forecolor;             };         }     } 

i having trouble figuring out how readonly rtb. how edit code display blue text on initialize in readonly rtb

thanks

there's no need subscribe textchanged event.

place code in separate method:

private void applysyntaxhighlite() {     string find = "using";     if (richtextbox1.text.contains(find))     {         var matchstring = regex.escape(find);         foreach (match match in regex.matches(richtextbox1.text, matchstring))         {             richtextbox1.select(match.index, find.length);             richtextbox1.selectioncolor = color.blue;             richtextbox1.select(richtextbox1.textlength, 0);             richtextbox1.selectioncolor = richtextbox1.forecolor;         };     } } 

and call after set text in richtextbox:

richtextbox1.text =     "using system;\r\nusing system.io;\r\n\r\nconsole.writeline(\"hello world.\");";  applysyntaxhighlite(); 

enter image description here


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -