c# - Avoid Scanning EntireRichtextbox Highlighting -
this question has answer here:
im working on code editor , came set of codes:
public class test2 : form { richtextbox m_rtb = null; public static void main() { application.run(new test2()); } public test2() { text = "test2"; clientsize = new size(400, 400); m_rtb = new richtextbox(); m_rtb.multiline = true; m_rtb.wordwrap = false; m_rtb.acceptstab = true; m_rtb.scrollbars = richtextboxscrollbars.forcedboth; m_rtb.dock = dockstyle.fill; m_rtb.selectionfont = new font("courier new", 10, fontstyle.regular); m_rtb.selectioncolor = color.black; controls.add(m_rtb); parse(); m_rtb.textchanged += new eventhandler(this.textchangedevent); } void parse() { string inputlanguage = "// comment.\n" + "using system;\n" + "\n" + "public class stuff : form { \n" + " public static void main(string args) {\n" + " }\n" + "}\n" ; // foreach line in input, // identify key words , format them when adding rich text box. regex r = new regex("\\n"); string [] lines = r.split(inputlanguage); foreach (string l in lines) { parseline(l); } } void parseline(string line) { regex r = new regex("([ \\t{}();])"); string [] tokens = r.split(line); foreach (string token in tokens) { // set token's default color , font. m_rtb.selectioncolor = color.black; m_rtb.selectionfont = new font("courier new", 10, fontstyle.regular); // check comment. if (token == "//" || token.startswith("//")) { // find start of comment , extract whole comment. int index = line.indexof("//"); string comment = line.substring(index, line.length - index); m_rtb.selectioncolor = color.lightgreen; m_rtb.selectionfont = new font("courier new", 10, fontstyle.regular); m_rtb.selectedtext = comment; break; } // check whether token keyword. string [] keywords = { "public", "void", "using", "static", "class" }; (int = 0; < keywords.length; i++) { if (keywords[i] == token) { // apply alternative color , font highlight keyword. m_rtb.selectioncolor = color.blue; m_rtb.selectionfont = new font("courier new", 10, fontstyle.bold); break; } } m_rtb.selectedtext = token; } m_rtb.selectedtext = "\n"; } private void textchangedevent(object sender, eventargs e) { // calculate starting position of current line. int start = 0, end = 0; (start = m_rtb.selectionstart - 1; start > 0; start--) { if (m_rtb.text[start] == '\n') { start++; break; } } // calculate end position of current line. (end = m_rtb.selectionstart; end < m_rtb.text.length; end++) { if (m_rtb.text[end] == '\n') break; } // extract current line being edited. string line = m_rtb.text.substring(start, end - start); // backup users current selection point. int selectionstart = m_rtb.selectionstart; int selectionlength = m_rtb.selectionlength; // split line tokens. regex r = new regex("([ \\t{}();])"); string [] tokens = r.split(line); int index = start; foreach (string token in tokens) { // set token's default color , font. m_rtb.selectionstart = index; m_rtb.selectionlength = token.length; m_rtb.selectioncolor = color.black; m_rtb.selectionfont = new font("courier new", 10, fontstyle.regular); // check comment. if (token == "//" || token.startswith("//")) { // find start of comment , extract whole comment. int length = line.length - (index - start); string commenttext = m_rtb.text.substring(index, length); m_rtb.selectionstart = index; m_rtb.selectionlength = length; m_rtb.selectioncolor = color.lightgreen; m_rtb.selectionfont = new font("courier new", 10, fontstyle.regular); break; } // check whether token keyword. string [] keywords = { "public", "void", "using", "static", "class" }; (int = 0; < keywords.length; i++) { if (keywords[i] == token) { // apply alternative color , font highlight keyword. m_rtb.selectioncolor = color.blue; m_rtb.selectionfont = new font("courier new", 10, fontstyle.bold); break; } } index += token.length; } // restore users current selection point. m_rtb.selectionstart = selectionstart; m_rtb.selectionlength = selectionlength; } }
problem everytime press space keys or type entire code editor keeps on scanning searching on highlight , find bit annoying ...
so want ask possible solution ... avoid highlighting of whole richtextbox scanning highlight next .
thanks lot in advance help! more power!
i answered in recent question, in case else reading , doesn't find it, i'll post here (since performance):
you can use couple of things improve performance:
1) can line user editing getting text range current selection. recommend using wpf's richtextbox contains more features , has useful textpointer class can utilise current line. seems using winforms however, can done these few lines of code (with addition of trivial code):
int start_index = rtb.getfirstcharindexofcurrentline(); int line = rtb.getlinefromcharindex(index); int last_index = rtb.getfirstcharindexfromline(line+1); rtb.select(start_index, last_index);
you can work current selection.
2) if don't want update frequently, can create timer measures delay since last edit, , reset timer if edit made before timer elapses.
Comments
Post a Comment