Nasıl yapılır: RichTextBox'dan Metin İçeriği Ayıklama
Bu örnekte, bir içeriğinin düz metin olarak nasıl RichTextBox ayıklan olduğu gösterir.
Örnek
Aşağıdaki Extensible Application Markup Language (XAML) kodu, basit içeriğe sahip RichTextBox adlandırılmış bir denetimi açıklar.
<RichTextBox Name="richTB">
<FlowDocument>
<Paragraph>
<Run>Paragraph 1</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 2</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 3</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
Örnek
Aşağıdaki kod, bağımsız değişken olarak alan ve düz metin içeriğini temsil eden bir dize döndüren RichTextBox bir yöntem RichTextBox kullanır.
yöntemi, ayıklanan TextRange içeriklerin aralığını belirtmek RichTextBox için ve kullanarak ContentStartContentEnd içindekilerden yeni bir oluşturur. ContentStart ve özelliklerine her biri bir döner ve içeriğini ContentEnd temsil eden temel TextPointer FlowDocument üzerinde RichTextBox erişilebilir. TextRange , dize olarak değerinin düz metin bölümlerini döndüren bir Text TextRange özelliği sağlar.
string StringFromRichTextBox(RichTextBox rtb)
{
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
rtb.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
rtb.Document.ContentEnd
);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}
Private Function StringFromRichTextBox(ByVal rtb As RichTextBox) As String
' TextPointer to the start of content in the RichTextBox.
' TextPointer to the end of content in the RichTextBox.
Dim textRange As New TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd)
' The Text property on a TextRange object returns a string
' representing the plain text content of the TextRange.
Return textRange.Text
End Function