c# - Cursor position inside a RichTextBox in WPF -
i developing small wpf application, has multiple tabs in it. have status bar in bottom; requirement show linenumber , column of cursor. when user changes cursor position, linenumber , column has updated automatically. here code add richtextbox; code calculates linenumber , column in keydown event handler, event never gets called. event should handle cursor linenumber , column?
private void addtabitem(string filepath, mode filemode) { if (filemode == mode.openfile) { if (file.exists(filepath)) { richtextbox mcrtb = new richtextbox(); mcrtb.keydown += new keyeventhandler(linenumber); //rest of code goes here } } } mcrtb.keydown += new keyeventhandler(linenumber); private void linenumber(object sender, keyeventargs e) { textpointer tp1 = rtblist[editortabcontrol.selectedindex].selection.start.getlinestartposition(0); textpointer tp2 = rtblist[editortabcontrol.selectedindex].selection.start; int column = tp1.getoffsettoposition(tp2); int somebignumber = int.maxvalue; int linemoved, currentlinenumber; rtblist[editortabcontrol.selectedindex].selection.start.getlinestartposition(-somebignumber, out linemoved); currentlinenumber = -linemoved; string linecolumnlabel; //linecolumnlabel.content = "line: " + currentlinenumber.tostring() + " column: " + column.tostring(); linecolumnlabel = "line: " + currentlinenumber.tostring() + " column: " + column.tostring(); }
there standard example task @ msdn (http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.caretposition%28v=vs.110%29.aspx). please note, in context 'cursor' called 'caret'. sample msdn follows:
// create new flowdocument, , add 3 paragraphs. flowdocument flowdoc = new flowdocument(); flowdoc.blocks.add(new paragraph(new run("paragraph 1"))); flowdoc.blocks.add(new paragraph(new run("paragraph 2"))); flowdoc.blocks.add(new paragraph(new run("paragraph 3"))); // set flowdocument content new richtextbox. richtextbox rtb = new richtextbox(flowdoc); // current caret position. textpointer caretpos = rtb.caretposition; // set textpointer end of current document. caretpos = caretpos.documentend; // specify new caret position @ end of current document. rtb.caretposition = caretpos;
Comments
Post a Comment