2010-11-08 1 views

Antwort

18

Am unteren Rande der MSDN RichTextBox Referenz ein Link

zu How to Extract the Text Content from a RichTextBox es Es wird wie folgt aussehen:

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: das ist ein bisschen kompliziert für etwas so einfach. Es ist nützlich, Anfang und Ende zu steuern, aber in den meisten Fällen nicht benötigt, und ich erwarte weiterhin .text oder .context usw. – Asaf

+0

@Asaf Ich denke nicht, dass das so kompliziert ist, die RichTextBox ist kein einfaches Textdokument. Einer RichTextBox sind Formatierungen, Stile usw. zugeordnet, und daher ist es sinnvoll, eine objektbasierte Unterstützung zu verwenden. –

+0

magst du recht haben, aber ich verliere meine Haare hier leise schnell: Grundlagen wie Text einstellen, clearText (= "") oder den String-Wert in eine Funktion setzen meiden mich.Es mag sinnvoll sein, aber es ist überhaupt nicht freundlich . – Asaf