RichTextBox.CaretPosition 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定輸入插入號 (Caret) 的位置。
public:
property System::Windows::Documents::TextPointer ^ CaretPosition { System::Windows::Documents::TextPointer ^ get(); void set(System::Windows::Documents::TextPointer ^ value); };
public System.Windows.Documents.TextPointer CaretPosition { get; set; }
member this.CaretPosition : System.Windows.Documents.TextPointer with get, set
Public Property CaretPosition As TextPointer
屬性值
TextPointer 物件,用來指定輸入插入號的位置。
根據預設,插入號會位於由 RichTextBox 所裝載之內容開頭的第一個插入位置。 如需像「插入位置」這類文字位置術語的詳細資訊,請參閱 TextPointer。
例外狀況
嘗試將這個屬性設定為 null
。
嘗試將這個屬性設定為 TextPointer,參考目前文件外部的某個位置。
範例
下列範例說明如何使用這個屬性。
// Create a new FlowDocument, and add 3 paragraphs.
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
// Set the FlowDocument to be the content for a new RichTextBox.
RichTextBox rtb = new RichTextBox(flowDoc);
// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;
// Set the TextPointer to the end of the current document.
caretPos = caretPos.DocumentEnd;
// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;
' Create a new FlowDocument, and add 3 paragraphs.
Dim flowDoc As New FlowDocument()
flowDoc.Blocks.Add(New Paragraph(New Run("Paragraph 1")))
flowDoc.Blocks.Add(New Paragraph(New Run("Paragraph 2")))
flowDoc.Blocks.Add(New Paragraph(New Run("Paragraph 3")))
' Set the FlowDocument to be the content for a new RichTextBox.
Dim rtb As New RichTextBox(flowDoc)
' Get the current caret position.
Dim caretPos As TextPointer = rtb.CaretPosition
' Set the TextPointer to the end of the current document.
caretPos = caretPos.DocumentEnd
' Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos