可正常工作,我得到I a
一段代碼:
RichTextBox rtb = new RichTextBox();
rtb.AppendText("I am adding some texts to the richTextBox");
rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
int coLen = 3;
TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(-coLen);
TextRange rtbText = new TextRange(caretBack, rtb.CaretPosition);
string ttt = rtbText.Text;
編輯
這裏是一個MSTest的方法來解釋符的行爲和閱讀:
[TestMethod]
public void TestRichtTextBox()
{
RichTextBox rtb = new RichTextBox();
rtb.AppendText("I am adding some texts to the richTextBox");
int offset = 3;
TextPointer beginningPointer = rtb.CaretPosition.GetPositionAtOffset(offset);
TextPointer endPointer = rtb.CaretPosition.DocumentEnd;
TextRange rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "m adding some texts to the richTextBox\r\n");
// Now we if we keep the same beggining offset but we change the end Offset to go backwards.
beginningPointer = rtb.CaretPosition.GetPositionAtOffset(3);
endPointer = rtb.CaretPosition; // this one is the beginning of the text
rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "I a");
// Nowe we want to read from the back three characters.
// so we set the end Point to DocumentEnd.
rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
beginningPointer = rtb.CaretPosition.GetPositionAtOffset(-offset);
endPointer = rtb.CaretPosition; // we already set this one to the end document
rtbText = new TextRange(beginningPointer, endPointer);
Assert.IsTrue(rtbText.Text == "Box");
}
加上這裏是從MSDN的負面指標註釋:
偏移類型:System.Int32的偏移,在符號,爲其 計算並傳回的位置。如果偏移量爲負數,則在邏輯方向上計算 位置,該邏輯方向與由LogicalDirection屬性指示的 相反。
Thanks for input.I should be getting「Box」because I need it to backwords from the end where the caret is。我希望它在脫字符前得到文本 – gumenimeda
檢查我的編輯,如果你想後退,你需要設置CaretPosition結束文檔。看起來後退不起作用,但是做-coLen有幫助。 – MBen
非常感謝!我花了很多時間研究這個! – gumenimeda