Có một số khá giải pháp tương đương GetPositionAtOffset()
chỉ tính vị trí chèn văn bản thay vì tất cả các ký hiệu không?GetPositionAtOffset tương đương cho chỉ văn bản?
Động lực dụ trong 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: Cho đến bây giờ tôi "giải quyết" nó theo cách này
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;
}
Wow ai đó REALLY không muốn RichTextBox được sử dụng .. cảm ơn! – gjvdkamp