Skip to content

Instantly share code, notes, and snippets.

@ambyte
Created January 14, 2015 11:19
Show Gist options
  • Select an option

  • Save ambyte/dc32cbe0dd47470801d4 to your computer and use it in GitHub Desktop.

Select an option

Save ambyte/dc32cbe0dd47470801d4 to your computer and use it in GitHub Desktop.
Get correct TextRange in richtextbox with newlines
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