Sunteți pe pagina 1din 5

How To:

Paste the Contents of the Clipboard as Plain Text in a .NET RichTextBox


DanieldotNET DEC2013 How to programmatically paste text in plain text format into a RichTextBox control. Software Version: Visual Basic 2008 Express Edition, .NET 3.5

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.

DataFormats Class and DataFormats.Format Class


The RichTextBox Class comes with two methods for pasting data from the Clipboard into the control: the Paste() method and the Paste(DataFormats.Format) method. The Paste() method simply pastes data from the Clipboard in whichever format the data is. On the other hand, Paste(DataFormats.Format) pastes data from the Clipboard only if it is in the format specified by the DataFormats.Format object passed into the method.

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.

Building the Demo Application


This Demo Application allows the user to paste the contents of the Clipboard on a RichTextBox control in Plain Text Format only. 1. Create a New Windows Application named CutCopyPasteDemo 2. Put three buttons and a RichTextBox control on Form1. Change the properties of each control as follows: Button1 Text = Cut Button2 Text = Copy Button3 Text = Paste RichTextBox1 ShortCutsEnabled = False Form1 Text = Cut Copy Paste Demo

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.

Your interface should resemble the one below.

Coding the Demo


3. Go to the Code Window and make your code look like the following Visual Basic Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click ' Ensures that text is currently selected in RichTextBox1 ' before moving the selected text to the Clipboard If RichTextBox1.SelectionLength > 0 Then ' Move the selected text to the Clipboard RichTextBox1.Cut() End If End Sub

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)

Running the Demo


Just press the F5 key to test your Demo. Go to the Code Window and copy the source code from there. The source code is formatted text. Now try to paste the contents of the clipboard using the paste button. The output below shows that the text that was copied to the clipboard is pasted to the RichTextBox unformatted.

S-ar putea să vă placă și