2010-11-08 50 views

回答

18

在MSDN RichTextBox參考的底部,有對How to Extract the Text Content from a RichTextBox

一個鏈接這將是這樣的:

public string RichTextBoxExample() 
{ 
    RichTextBox myRichTextBox = new RichTextBox(); 

    // Create a FlowDocument to contain content for the RichTextBox. 
    FlowDocument myFlowDoc = new FlowDocument(); 

    // Add initial content to the RichTextBox. 
    myRichTextBox.Document = myFlowDoc; 

    // Let's pretend the RichTextBox gets content magically ... 

    TextRange textRange = new TextRange(
     // TextPointer to the start of content in the RichTextBox. 
     myRichTextBox.Document.ContentStart, 
     // TextPointer to the end of content in the RichTextBox. 
     myRichTextBox.Document.ContentEnd 
    ); 

    // The Text property on a TextRange object returns a string 
    // representing the plain text content of the TextRange. 
    return textRange.Text; 
} 
+2

+1:這對於如此基本的事情來說有點複雜。在大多數時間不需要控制開始和結束是很有用的,我仍然期望.text或.context等。 – Asaf 2010-11-08 15:55:29

+0

@Asaf我不認爲這很複雜,RichTextBox不是純文本文檔。有與RichTextBox相關聯的格式,樣式等,因此具有基於對象的支持是有意義的。 – 2010-11-08 15:59:13

+0

你可能是對的,但我在這裏放鬆我的頭髮安靜快速:像設置文本,clearText(=「」),或將字符串值放在函數中的基礎知識都避免了我。它可能是有道理的,但它根本不友好。 – Asaf 2010-11-08 16:10:41

相關問題