Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Mastering the WPF RichTextBox : Page 2

Nearly everything about the the newest version of the RichTextBox has changed except its purpose as a formatting text editor control. Find out how you can take advantage of the new features.


advertisement
Getting the Data Out
Now that you've got a WPF RichTextBox on your page and supporting some nifty edit commands, you need to find a way to get to the document content that users have entered into the control. Seasoned programmers probably got lockjaw from the moment they realized this was a big enough topic to warrant an entire section. Unfortunately, it does warrant an entire section.

The RichTextBox works with XAML natively to build out all of the text effects you see and enable the capabilities that were absent from previous versions. When you read the value (contents), what you'll get is XAML code. A large contingent of the application development community probably has all of their rich formatted data stored in HTML format for portability purposes and will have to write their own HTML ( XAML ( HTML conversion routines. Although not complete, a Microsoft Channel 9 blogger created a reference implementation routine for doing just this that you can download . It's not perfect but it saves you from having to build the routine yourself for simple HTML/XAML conversions.

To get the actual XAML created by the user inside of the RichTextBox, you create a TextRange, and then do some stream processing to read the XAML. The example below uses a memory stream to read the value from the control, and then uses the ASCIIEncoding object to translate it to a string for storage in a database, file, or other data store:

TextRange tr = new TextRange( myRichTextBox.Document.ContentStart, myRichTextBox.Document.ContentEnd); MemoryStream ms = new MemoryStream(); tr.Save(ms, DataFormats.Xaml); string xamlString = ASCIIEncoding.Default.GetString(ms.ToArray());

The preceding code retrieves the entire contents of the RichTextBox by creating a TextRange that starts at the beginning of the document and extends all the way to the end. Although this is more complicated than just getting the value from a Text or Value property it enables you to create much more complex applications using the RichTextBox as a base. You can create smaller text ranges that cover specific portions of a rich text document to develop different functionality.

Modifying the Interface
 
Figure 3. A Date Button: The Date button added to the toolbar inserts the current date into a TextRange.
One common scenario would be to add a button to your control that might get some data from a database and dynamically insert it into the RichTextBox document. This simplified example just inserts the current date.

You use the TextRange object to select what part of the RichTextBox's document to update. In Figure 3, a toolbar button lets users insert the current date.

In the Click event handler for the button, create a text range based on the current selection, using the Selection.Start and Selection.End properties. Setting the Text property of this TextRange to the current date string replaces whatever text is currently selected. If you just wanted the date to be inserted at the current cursor position without replacing anything, you would create the TextRange using the Selection.Start value for both the starting and ending point of your TextRange.

protected void InsertCurrentDate (object sender, EventArgs e) { TextRange tr = new TextRange( XAMLRichBox.Selection.Start, XAMLRichBox.Selection.End); tr.Text = DateTime.Now.ToShortDateString(); }

Creative use of TextRange based updating allows multiple users to change the same RichTextBox in real-time though the internet as well as any number of other creative possibilities, such as inline word suggestion similar to IntelliSense.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap