SlideShare a Scribd company logo
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.

More Related Content

What's hot

05.formatting cells
05.formatting cells05.formatting cells
05.formatting cells
Steven Alphonce
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
BG Java EE Course
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
Ahmed Yaseen
 
Common dialog control
Common dialog controlCommon dialog control
Common dialog control
Soumya Vijoy
 
Oracle Inventory Restrict user from update item attribute personalization
Oracle Inventory Restrict user from update item attribute personalizationOracle Inventory Restrict user from update item attribute personalization
Oracle Inventory Restrict user from update item attribute personalization
Ahmed Elshayeb
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
Aarti P
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base
Salman Memon
 
Spreadsheet text functions
Spreadsheet text functionsSpreadsheet text functions
Spreadsheet text functions
Anjan Mahanta
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
SQL
SQLSQL
Oracle report from ppt
Oracle report from pptOracle report from ppt
Oracle report from ppt
kingshuk_goswami
 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
Palitha Baddegama
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
Shrija Madhu
 
Sql commands
Sql commandsSql commands
Sql commands
Pooja Dixit
 
Oracle EBS(On Premise) and Oracle Revenue Management Cloud Integration
Oracle EBS(On Premise) and Oracle Revenue Management Cloud IntegrationOracle EBS(On Premise) and Oracle Revenue Management Cloud Integration
Oracle EBS(On Premise) and Oracle Revenue Management Cloud Integration
Bizinsight Consulting Inc
 
ch14.ppt
ch14.pptch14.ppt
ch14.ppt
ssuser67281d
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
Salman Memon
 
Formulas and functions - By Amresh Tiwari
Formulas and functions - By Amresh TiwariFormulas and functions - By Amresh Tiwari
Formulas and functions - By Amresh Tiwari
Amresh Tiwari
 
Database Testing.pptx
Database Testing.pptxDatabase Testing.pptx
Database Testing.pptx
ssuser88c0fd1
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 

What's hot (20)

05.formatting cells
05.formatting cells05.formatting cells
05.formatting cells
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
Common dialog control
Common dialog controlCommon dialog control
Common dialog control
 
Oracle Inventory Restrict user from update item attribute personalization
Oracle Inventory Restrict user from update item attribute personalizationOracle Inventory Restrict user from update item attribute personalization
Oracle Inventory Restrict user from update item attribute personalization
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base
 
Spreadsheet text functions
Spreadsheet text functionsSpreadsheet text functions
Spreadsheet text functions
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
SQL
SQLSQL
SQL
 
Oracle report from ppt
Oracle report from pptOracle report from ppt
Oracle report from ppt
 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Sql commands
Sql commandsSql commands
Sql commands
 
Oracle EBS(On Premise) and Oracle Revenue Management Cloud Integration
Oracle EBS(On Premise) and Oracle Revenue Management Cloud IntegrationOracle EBS(On Premise) and Oracle Revenue Management Cloud Integration
Oracle EBS(On Premise) and Oracle Revenue Management Cloud Integration
 
ch14.ppt
ch14.pptch14.ppt
ch14.ppt
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Formulas and functions - By Amresh Tiwari
Formulas and functions - By Amresh TiwariFormulas and functions - By Amresh Tiwari
Formulas and functions - By Amresh Tiwari
 
Database Testing.pptx
Database Testing.pptxDatabase Testing.pptx
Database Testing.pptx
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 

Similar to Paste Only Plain Text in RichTextBox Control using Visual Basic.Net

I x scripting
I x scriptingI x scripting
I x scripting
Alex do Amaral Dias
 
Bt0082 visual basic2
Bt0082 visual basic2Bt0082 visual basic2
Bt0082 visual basic2
Techglyphs
 
Sharbani bhattacharya VB Structures
Sharbani bhattacharya VB StructuresSharbani bhattacharya VB Structures
Sharbani bhattacharya VB Structures
Sharbani Bhattacharya
 
Textbox n label
Textbox n labelTextbox n label
Textbox n label
chauhankapil
 
Android interface elements and controls-chapter8
Android interface elements and controls-chapter8Android interface elements and controls-chapter8
Android interface elements and controls-chapter8
Dr. Ramkumar Lakshminarayanan
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3
Bhushan Mulmule
 
Mca 504 dotnet_unit5
Mca 504 dotnet_unit5Mca 504 dotnet_unit5
Winforms
WinformsWinforms
Combo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.pptCombo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.ppt
Ujwala Junghare
 
VB Dot net
VB Dot net VB Dot net
VB Dot net
Akber Khowaja
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
J. C.
 
Html Tutorial
Html TutorialHtml Tutorial
Html Tutorial
Md. Muhibbullah Muhib
 
Bt0082 visual basic
Bt0082 visual basicBt0082 visual basic
Bt0082 visual basic
Techglyphs
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
Ali Mattash
 
GWT Widgets
GWT WidgetsGWT Widgets
Automation Anywhere Examples
Automation Anywhere ExamplesAutomation Anywhere Examples
Automation Anywhere Examples
Shekar S
 
Lesson 2 data preprocessing
Lesson 2   data preprocessingLesson 2   data preprocessing
Lesson 2 data preprocessing
AbdurRazzaqe1
 
Tugas testing
Tugas testingTugas testing
Tugas testing
Astrid yolanda
 
Visual basic bt0082
Visual basic  bt0082Visual basic  bt0082
Visual basic bt0082
Divyam Pateriya
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
Andi Simanjuntak
 

Similar to Paste Only Plain Text in RichTextBox Control using Visual Basic.Net (20)

I x scripting
I x scriptingI x scripting
I x scripting
 
Bt0082 visual basic2
Bt0082 visual basic2Bt0082 visual basic2
Bt0082 visual basic2
 
Sharbani bhattacharya VB Structures
Sharbani bhattacharya VB StructuresSharbani bhattacharya VB Structures
Sharbani bhattacharya VB Structures
 
Textbox n label
Textbox n labelTextbox n label
Textbox n label
 
Android interface elements and controls-chapter8
Android interface elements and controls-chapter8Android interface elements and controls-chapter8
Android interface elements and controls-chapter8
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3
 
Mca 504 dotnet_unit5
Mca 504 dotnet_unit5Mca 504 dotnet_unit5
Mca 504 dotnet_unit5
 
Winforms
WinformsWinforms
Winforms
 
Combo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.pptCombo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.ppt
 
VB Dot net
VB Dot net VB Dot net
VB Dot net
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
Html Tutorial
Html TutorialHtml Tutorial
Html Tutorial
 
Bt0082 visual basic
Bt0082 visual basicBt0082 visual basic
Bt0082 visual basic
 
Visual C# 2010
Visual C# 2010Visual C# 2010
Visual C# 2010
 
GWT Widgets
GWT WidgetsGWT Widgets
GWT Widgets
 
Automation Anywhere Examples
Automation Anywhere ExamplesAutomation Anywhere Examples
Automation Anywhere Examples
 
Lesson 2 data preprocessing
Lesson 2   data preprocessingLesson 2   data preprocessing
Lesson 2 data preprocessing
 
Tugas testing
Tugas testingTugas testing
Tugas testing
 
Visual basic bt0082
Visual basic  bt0082Visual basic  bt0082
Visual basic bt0082
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
 

Recently uploaded

Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
Luigi Fugaro
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
KrishnaveniMohan1
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
vaishalijagtap12
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
OnePlan Solutions
 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ICS
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdfSoftware Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
kalichargn70th171
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
OnePlan Solutions
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
jrodriguezq3110
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
kalichargn70th171
 

Recently uploaded (20)

Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdfSoftware Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
 

Paste Only Plain Text in RichTextBox Control using Visual Basic.Net

  • 1. 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.
  • 2. 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.
  • 3. 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
  • 4. 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
  • 5. 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.