Created
January 14, 2015 11:19
-
-
Save ambyte/dc32cbe0dd47470801d4 to your computer and use it in GitHub Desktop.
Get correct TextRange in richtextbox with newlines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var start = reachTextBox.Document.ContentStart; | |
var textrange = new TextRange(GetPoint(start, pos), GetPoint(start, pos + len)); | |
private TextPointer GetPoint(TextPointer start, int x) | |
{ | |
var ret = start; | |
var i = 0; | |
while (true) | |
{ | |
string stringSoFar = new TextRange(ret, ret.GetPositionAtOffset(i, LogicalDirection.Forward)).Text; | |
if (stringSoFar.Length == x) | |
break; | |
i++; | |
if (ret.GetPositionAtOffset(i, LogicalDirection.Forward) == null) | |
return ret.GetPositionAtOffset(i - 1, LogicalDirection.Forward); | |
} | |
ret=ret.GetPositionAtOffset(i, LogicalDirection.Forward); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment