The RichTextBox control provides all the text-editing features you’d expect to find in a text-editing application, similar to the TextBox control. Among its more-advanced features, the RichTextBox control provides the AutoWordSelection property, which controls how the control selects text. If it’s True, the control selects a word at a time.
In addition to formatted text, the RichTextBox control can handle object linking and embedding (OLE) objects. You can insert images in the text by pasting them with the Paste method. The Paste method doesn’t require any arguments; it simply inserts the contents of the Clipboard at the current location in the document.
The RichTextBox control encapsulates undo and redo operations at multiple levels. Each operation has a name (Typing, Deletion, and so on), and you can retrieve the name of the next operation to be undone or redone and display it on the menu. Instead of a simple Undo or Redo caption, you can change the captions of the Edit menu to something like Undo Delete or Redo Typing. To program undo and redo operations from within your code, you must use the properties and methods discussed in the following sections.
These two properties are Boolean values you can read to find out whether there’s an operation that can be undone or redone. If they’re False, you must disable the corresponding menu command from within your code. The following statements disable the Undo command if there’s no action to be undone at the time (EditUndo is the name of the Undo command on the Edit menu):
If RichTextBox1.CanUndo Then EditUndo.Enabled = True Else EditUndo.Enabled = False End IfCode language: VB.NET (vbnet)
These statements should appear in the menu item’s Select event handler (not in the Click event handler) because they must be executed before the menu is displayed. The Select event is triggered when a menu is opened. As a reminder, the Click event is fired when you click an item, and not when you open a menu. For more information on programming the events of a menu, see Chapter, “Working with Forms.”
These two properties return the name of the action that can be undone or redone. The most common value of both properties is Typing, which indicates that the Undo command will delete a number of characters. Another common value is Delete, whereas some operations are named Unknown. If you change the indentation of a paragraph on the control, this action’s name is Unknown. Even when an action’s name is Unknown, the action can be undone with the Undo method.
The following statement sets the caption of the Undo command to a string that indicates the action to be undone (Editor is the name of a RichTextBox control):
If Editor.CanUndo Then EditUndo.Text = "Undo " & Editor.UndoActionName End IfCode language: VB.NET (vbnet)
These two methods undo or redo an action. The Undo method cancels the effects of the last action of the user on the control. The Redo method redoes the most recent undo action. The Redo method does not repeat the last action; it applies to undo operations only.
Cutting and Pasting
To cut, copy, and paste text in the RichTextBox control, you can use the same techniques you use with the regular TextBox control. For example, you can replace the current selection by assigning a string to the SelectedText property. The RichTextBox, however, provides a few useful methods for performing these operations. The Copy, Cut, and Paste methods perform the corresponding operations. The Cut and Copy methods are straightforward and require no arguments. The Paste method accepts a single argument, which is the format of the data to be pasted. Because the data will come from the Clipboard, you can extract the format of the data in the Clipboard at the time and then call the CanPaste method to find out whether the control can handle this type of data. If so, you can then paste them in the control by using the Paste method.
This technique requires a bit of code because the Clipboard class doesn’t return the format of the data in the Clipboard. You must call the following method of the Clipboard class to find out whether the data is of a specific type and then paste it on the control:
If Clipboard.GetDataObject. _ GetDataPresent(DataFormats.Text) Then RichTextBox.Paste(DataFormats.Text) End IfCode language: VB.NET (vbnet)
This is a very simple case because we know that the RichTextBox control can accept text. For a robust application, you must call the GetDataPresent method for each type of data your application should be able to handle. (You may not want to allow users to paste all types of data that the control can handle.) By the way, you can simplify the code with the help of the ContainsText/ContainsImage and GetText/GetImage methods of the My.Application.Clipboard object.
In the RichTextBoxPad project later in this chapter, we’ll use a structured exception handler to allow users to paste anything in the control. If the control can’t handle it, the data won’t be pasted in the control.