text - Matching brackets and indentation in RichTextBox using C#? -


i want make code editing control can format text visual studio,till have implemented syntax highlighting , autocompletion want format text in nested curly braces.for example:consider loop,

for(int i=0;i<=10;i++) { function_one();              //this should tab away first brace function_two();              //so if(a==b)                     //so {                            //this should 4 tabs away first brace messagebox.show("some text");//this should 6 tabs away first brace }                            //this should 4 tabs away first brace } 

now want should this,

for(int i=0;i<=10;i++) {    function_one();                  function_two();                  if(a==b)                         {                                   messagebox.show("some text");    }                             } 

i have tried regular expressions @ point fail match,so tried match code code cannot match nested code or hard implement ,so there way achieve this,and 1 more thing doing in winforms control richtextbox using c#.

i think best way implement create global variables form:

private int _nestedbracketcount = 0; private const string tabstring = "    "; private const int tabspaces = 4; 

and handle of in keypressed event handler rich text box:

private void richtextbox1_onkeypress(object sender, keypresseventargs e) {     var currentlength = richtextbox1.text.length;      if (e.keychar == '{') {         // need increment bracket counter         _nestedbracketcount++;     } else if (e.keychar == '}') {         // remove last #(tabspaces) characters richtextbox         richtextbox1.text.remove(currentlength - tabspaces);         _nestedbracketcount--;         richtextbox1.appendtext("}");         e.handled = true;     } else if (e.keychar == (char)13) {         // append newline , correct number of tabs.         var formattedspaces = string.empty;         (var = 0; < _nestedbracketcount; i++)             formattedspaces += tabstring;         richtextbox1.appendtext("\n" + formattedspaces);         e.handled = true;     } } 

i think should provide halfway decent starting point.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -