How to copy paste plain text in RichTextBox in C#
By Tan Lee Published on Jun 08, 2024 8.2K
In a C# Windows Forms application, you can copy and paste plain text in a RichTextBox control by manipulating the Text property directly.
How to copy paste plain text in RichTextBox in C#
Create a new Windows Forms Application project, then drag and drop the RichTextBox control from your Visual Studio toolbox to your form designer.
By default, Instead of pasting the text with format. We can extract the plain text, then add it into the RichTextBox control.
Clicking on RichTextBox control, then select properties.
Adding a KeyDown event handler allows you to paste text into a RichTextBox control.
private void richTextBox_KeyDown(object sender, KeyEventArgs e) { // Copy the selected text as plain text if (e.Control && e.KeyCode == Keys.V) { // Paste plain text from the clipboard richTextBox.Text += (string)Clipboard.GetData("Text"); e.Handled = true; } }
This code will copy and paste plain text, stripping any formatting from the RichTextBox
. Each time you copy data, it will be stored in the Clipboard. To get plain text to can call GetData("Text") method.
- Implementing Copy, Cut, and Paste Context Menu in a Rich Text Box in C#
- How to allow only plain text inside a RichTextBox in C#
- How to Use Multiple Color in RichTextBox using C#
- How to Search and Highlight Text in a RichTextBox using C#
- How to Autocomplete RichTextBox in C#
- How to Insert image into RichTextBox in C#
Categories
Popular Posts
Portal HTML Bootstrap
Nov 13, 2024
Freedash bootstrap lite
Nov 13, 2024
Motiv MUI React Admin Dashboard Template
Nov 19, 2024
K-WD Tailwind CSS Admin Dashboard Template
Nov 17, 2024