How To Paste Plain Text Only in RichTextbox Control (Visual Basic)
How To Paste Plain Text Only in RichTextbox Control (Visual Basic)
Contents
Introduction DataFormats Class and DataFormats.Format Class Building the Demo Application Coding the Demo Running the Demo Application
Introduction
Pasting text from the clipboard is a common operation in word processing. It is also possible to paste richly formatted text from the clipboard into a RichTextBox control. This is the default behavior: just press Ctrl+V and the richly formatted text is on the RichTextBox. However, there are applications that need the RichTextBox to accept unformatted text only. For example when creating a Notepad application, one might want to restrict the contents to plain text. This short article is about how it can be done programmatically.
A DataFormats.Format object stores the name and id of a specific Clipboard format. Passing this information into the Paste method causes it to paste the Clipboard data if the clipboard data is in the specified format. However, it is possible to first find out if the Clipboard data is in the specified format by calling the RichTextBox.CanPaste method. CanPaste also requires a DataFormats.Format argument and returns True if the Clipboard data is in the specified format. To solve the problem of knowing the specific names and ids of the common data formats for example bitmap, text, Unicode text, rich text, html the DataFormats class provides static, predefined members representing the common data formats. In order to construct a DataFormats.Format object, however, the name of the required data format (one of the membwers of the DataFormats class) can be passed into the DataFormats.GetFormat method. This method will use the entered information to construct the required object which can then be used by the CanPaste and Paste methods of the RichTextBox object.
Note If the ShortCutsEnabled property is set to True, it allows the shortcut key combination Ctrl+V (for paste) to be active on the RichTextBox. When Ctrl+V is pressed during runtime, it pastes any content from the clipboard, including rich text and pictures. So to control this behavior, set the property to False.
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button2.Click ' Ensures that text is currently selected in RichTextBox1 ' before moving the selected text to the Clipboard If RichTextBox1.SelectionLength > 0 Then ' Copy the selected text to the Clipboard RichTextBox1.Copy() End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button3.Click ' Define a PlainText data format Dim PlainTextFormat As DataFormats.Format = _ DataFormats.GetFormat(DataFormats.Text) ' First verify that the data in the Clipboard is plaintext ' format before pasting If RichTextBox1.CanPaste(PlainTextFormat) Then ' Paste the contents of the Clipboard to RichTextBox1 RichTextBox1.Paste(PlainTextFormat) End If End Sub
The code above has three event handling methods a. Button1_Click contains logic for moving text to the clipboard. Notice that the Cut method of the RichTextBox is wrapped with an if-statement. This is because nothing will be moved to the clipboard if no text is selected. To find out if text is selected in the RichTextBox, the if statement checks the SelectionLength property of the RichTextBox, which will be zero if no text is selected. b. Button2_Click contains logic for copying text to the clipboard. Notice the similarity between Button1_Click and Button2_Click. If no text is selected, nothing will be copied to the clipboard. c. Button3_Click contains the pasting logic. The DataFormats.Format variable plainTextFormat stores the text data format
Visual Basic
Dim PlainTextFormat As DataFormats.Format = _ DataFormats.GetFormat(DataFormats.Text) The contents of the plainTextFormat variable is then passed to the Paste method Visual Basic RichTextBox1.Paste(PlainTextFormat)