Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
En este ejemplo se muestra cómo extraer el contenido de un RichTextBox como texto sin formato.
Describir un control RichTextBox
El siguiente código de Lenguaje de marcado extensible de aplicaciones (XAML) describe un control con nombre RichTextBox con contenido simple.
<RichTextBox Name="richTB">
<FlowDocument>
<Paragraph>
<Run>Paragraph 1</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 2</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 3</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
Ejemplo de código con RichTextBox como argumento
El código siguiente implementa un método que toma un RichTextBox como argumento y devuelve una cadena que representa el contenido de texto sin formato del RichTextBox.
El método crea un nuevo TextRange a partir del contenido del RichTextBox, utilizando ContentStart y ContentEnd para indicar el rango del contenido que se va a extraer. ContentStart y ContentEnd las propiedades cada una devuelve un TextPointer y son accesibles en el FlowDocument subyacente que representa el contenido del RichTextBox. TextRange proporciona una propiedad Text, que devuelve las partes de texto sin formato de TextRange como una cadena.
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
Consulte también
- Información general sobre RichTextBox
- Información general sobre TextBox
.NET Desktop feedback