c# - Wpf Richtextbox selection starting column and row number returns different values -
i initializing richtextbox,
void initrtbflowdocument() { style nospacestyle = new style(typeof(paragraph)); nospacestyle.setters.add(new setter(paragraph.marginproperty, new thickness(0))); rtbtexteditor.resources.add(typeof(paragraph), nospacestyle); }
i want richtext box selection words row , column numbers. wrote code follows, first time returning correctly.
void rtbtexteditor_selectionchanged(object sender, routedeventargs e) { //get richtext box selected text init.rtbselectiontext = rtbtexteditor.selection.text.trim(); init.selectiontext = rtbtexteditor.selection.text.trim(); init.isselect = true; if (init.rtbselectiontext != string.empty) { textpointer tp = rtbtexteditor.selection.start.getpositionatoffset(-2, logicaldirection.forward); if (tp != null) getstartingindex(); } init.rtbcontent = new textrange(rtbtexteditor.document.contentstart, rtbtexteditor.document.contentend).text; } void getstartingindex() { textpointer tp1 = rtbtexteditor.selection.start.getlinestartposition(0); textpointer tp2 = rtbtexteditor.selection.start; int selectioncolumnindex = tp1.getoffsettoposition(tp2)-1;//column number int somebignumber = int.maxvalue; int linemoved; rtbtexteditor.selection.start.getlinestartposition(-somebignumber, out linemoved); //line number int selectionrowindex = -linemoved; init.rtbtextpoint = new rtbtextpointer(); init.rtbtextpoint.row = selectionrowindex; init.rtbtextpoint.column = selectioncolumnindex; }
after clearing , added new content, position returns wrong number,
public void displaycontent(string content) { //clear rich text box rtbtexteditor.document.blocks.clear(); rtbtexteditor.document.blocks.add(new paragraph(new run(content))); }
is rong in above code. please me in advance!
this because contents in rtb not contain text, contains these things called textpointercontext's. textpointer's take account. can check textpointer adjacent using:
textpointer.getpointercontext(logicaldirection);
to next textpointer:
textpointer.getnextcontextposition(logicaldirection);
some sample code used in recent project, makes sure pointer context of type text:
while (start.getpointercontext(logicaldirection.forward) != textpointercontext.text) { start = start.getnextcontextposition(logicaldirection.forward); if (start == null) return; }
when clear rtb, you're removing of these pointer contexts. careful when using getpositionatoffset(). should give "pointer" in right direction. if need more me know.
good hunting!
Comments
Post a Comment