Showing posts with label RichTextBox. Show all posts
Showing posts with label RichTextBox. Show all posts

Wednesday, March 9, 2011

RichTextBox

The RichTextBox is much more complex than the TextBox because it stores text that could have a variety of different character and paragraph formatting. If you're familiar with previous Windows controls similar to RichTextBox, you might naturally assume that the control stores text in the Rich Text Format (RTF), a file format that dates from the mid-1980s and which was intended to provide an interchange format among word processing programs. The WPF version of RichTextBox certainly supports RTF (and plain text as well) but it also supports an XML-based file format that is a subset of the Extensible Application Markup Language (XAML) supported by WPF.

Formatted text gets into and out of the RichTextBox through the Document property. This Document property is of type FlowDocument, and the following code creates a TextRange object that encompasses the entire document:

TextRange range = new TextRange(flow.ContentStart, flow.ContentEnd);
The TextRange class defines two methods named Load and Save. The first argument is a Stream; the second is a string describing the desired data format. For this second argument it's easiest to get those strings from static members of the DataFormats class. With the RichTextBox, you can use DataFormats.Text, DataFormats.Rtf, DataFormats.Xaml, and DataFormats.XamlPackage, which is actually a ZIP file that contains binary resources the document might require. This program is hard-coded to use DataFormats.Xaml. (The program can't load any arbitrary XAML file, however.) You may want to look at these files to get a glimpse of the format of documents under the Windows Presentation Foundation.


TextBox and RichTextBox (Code)

Speaking of the TextBox control, the Windows Presentation Foundation supports the two we've come to expect:
Control
       TextBoxBase (abstract)
                  TextBox
                  RichTextBox
Although the TextBox has content, it is not considered a ContentControl because the content is always text. Instead it has a Text property that lets you set an initial string or obtain the string that the user typed.
Because TextBox derives from Control, you can specify the background and foreground brushes used in the control, as well as the font. However, at any time, all the text that appears in a particular TextBox will always be the same color and use the same font. The RichTextBox control lets the program (and the user) apply various character and paragraph formatting to different parts of the text. The difference between TextBox and RichTextBox is the same as the difference between Windows Notepad and Windows WordPad.
By default, a TextBox control allows entry of a single line of text.

By default the TextBox isn't interested in the Enter key. If you set :
Label lbl = new Label();
lbl.Content = "File _name:";
The single-line edit control becomes a multiline edit control and now handles carriage returns.