2010-11-08 47 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

相关问题