wpf - GetPositionAtOffset equivalent for text only? -


there pretty solution of getpositionatoffset() equivalent counts text insertion positions instead of symbols?

motivation example in c#:

textrange getrange(richtextbox rtb, int startindex, int length) {     textpointer startpointer = rtb.document.contentstart.getpositionatoffset(startindex);     textpointer endpointer = startpointer.getpositionatoffset(length);     return new textrange(startpointer, endpointer); } 

edit: until "solved" way

public static textpointer getinsertionpositionatoffset(this textpointer position, int offset, logicaldirection direction) {     if (!position.isatinsertionposition) position = position.getnextinsertionposition(direction);     while (offset > 0 && position != null)     {         position = position.getnextinsertionposition(direction);         offset--;         if (environment.newline.length == 2 && position != null && position.isatlinestartposition) offset --;      }     return position; } 

as far i'm aware, there isn't. suggestion create own getpositionatoffset method purpose. can check pointercontext textpointer adjacent using:

textpointer.getpointercontext(logicaldirection); 

to next textpointer points different pointercontext:

textpointer.getnextcontextposition(logicaldirection); 

some sample code used in recent project, makes sure pointer context of type text, looping until 1 found. use in implementation , skip offset increment if found:

// textpointer start  while (start.getpointercontext(logicaldirection.forward)                               != textpointercontext.text) {     start = start.getnextcontextposition(logicaldirection.forward);     if (start == null) return; } 

hopefully can make use of information.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -