richtextbox - C# writing a code editor issues -
so i'm writing simple code editor language like. have syntax hilighting going well. problem if go before text have written, screws whole hilighting past pointer. here code, apologies posting much:
public partial class form1 : form { public string mainfontname = "courier new"; public int mainfontsize = 12; public color mainfontcolor = color.black; [dllimport("user32.dll")] // import lockwindow remove flashing public static extern bool lockwindowupdate(intptr hwndlock); public regex codefunctions = new regex("draw_line|draw_rectangle|draw_circle"); public regex codekeywords = new regex("and|for|while|repeat|or|xor|exit|break|case|switch|if|then|with|true|false"); public form1() { initializecomponent(); codeinput.font = new font(mainfontname, mainfontsize, fontstyle.regular); } private void codeinput_textchanged(object sender, eventargs e) { codeinput.font = new font(mainfontname, mainfontsize, fontstyle.regular); try { lockwindowupdate(codeinput.handle); int selpos = codeinput.selectionstart; codeinput.select(0, codeinput.textlength); codeinput.selectionfont = new font(mainfontname, mainfontsize, fontstyle.regular); codeinput.selectioncolor = color.black; codeinput.selectionlength = 0; codeinput.selectionstart = selpos; //match functions foreach (match keywordmatch in codefunctions.matches(codeinput.text)) { codeinput.select(keywordmatch.index, keywordmatch.length); codeinput.selectioncolor = color.red; codeinput.selectionstart = selpos; codeinput.selectioncolor = mainfontcolor; codeinput.selectionlength = 0; } // match keywords foreach (match keywordmatch in codekeywords.matches(codeinput.text)) { font ofont = new font(mainfontname, mainfontsize, fontstyle.bold); font nfont = new font(mainfontname, mainfontsize, fontstyle.regular); codeinput.select(keywordmatch.index, keywordmatch.length); codeinput.selectioncolor = color.blue; codeinput.selectionfont = ofont; codeinput.selectionstart = selpos; codeinput.selectioncolor = mainfontcolor; codeinput.selectionfont = nfont; codeinput.selectionlength = 0; } } { lockwindowupdate(intptr.zero); } } }
thanks help.
you on right track try blocking wm_paint instead of using api calls,i have worked on type of project , implemented syntax highlighting successfully,its original source code below;
///radoncodeeditor name of project /// <summary> /// gets or sets value whether radontexteditor should repaint itself. /// </summary> public bool repaint = true; ckeyword=color.blue; ccomment=color.green; /// <summary> /// windows generated message send control needs repainting. /// </summary> const short wm_paint = 0x00f; /// <summary> /// contains creation data call radontexteditor. /// </summary> public radontexteditor() { setstyle(controlstyles.optimizeddoublebuffer, true);//reduces flickering. } /// <summary> /// overrides default wndproc , handles wm_paint message remove flickering. /// </summary> /// <param name="m">the message in message queue.</param> protected override void wndproc(ref message m) { if (m.msg == wm_paint)//if message in message queue wm_paint. { switch (repaint) { case true://when want radontexteditor repaint. base.wndproc(ref m); break; case false://when don't want radontexteditor repaint. m.result = intptr.zero; break; } } else//if message in message queue else not wm_paint. { base.wndproc(ref m); } } //import namespace system.text.regularexpressions /// <summary> /// checks input text contained comments using defined regular expression. /// </summary> /// <param name="text">the text check.</param> /// <param name="firstcharindex">the first character index of current line.</param> /// <param name="caretposition">the position of caret.</param> /// <param name="rtb">the handle richtextbox highlighting.</param> /// <returns>if input text contains text return value true otherwise false.</returns> public void iscomment(string text, int firstcharindex, int caretposition, richtextbox rtb) { rcomment = new regex(@"\/\/.*$", regexoptions.compiled); matchcollection matches = rcomment.matches(text); foreach (match match in matches) { rtb.select(match.index + firstcharindex, match.length); rtb.selectioncolor = ccomment; rtb.deselectall(); rtb.selectionstart = caretposition; } rmulticomment = new regex(@"/\*.*?\*/", regexoptions.multiline); matchcollection matches2 = rmulticomment.matches(text); foreach (match match2 in matches2) { rtb.select(match2.index + firstcharindex, match2.length); rtb.selectioncolor = ccomment; rtb.deselectall(); rtb.selectionstart = caretposition; } } /// <summary> /// checks input text contained keywords using defined regular expression. /// </summary> /// <param name="text">the text check.</param> /// <param name="firstcharindex">the first character index of current line.</param> /// <param name="caretposition">the position of caret.</param> /// <param name="rtb">the handle richtextbox highlighting.</param> /// <returns>if input text contains text return value true otherwise false.</returns> public void iskeyword(string text, int firstcharindex, int caretposition, richtextbox rtb) { rkeyword = new regex(@:\bint\b|\bdouble\b|\bstring\b", regexoptions.compiled); matchcollection matches = rkeyword.matches(text); foreach (match match in matches) { rtb.select(match.index + firstcharindex, match.length); rtb.selectioncolor = ckeyword; rtb.deselectall(); rtb.selectionstart = caretposition; } }
that's it,call both functions in textchange event handler , pass required arguments functions described comments(i know it's bit messy because have directly pasted source code project have.)if still goes wrong i'll glad you.
Comments
Post a Comment