How to copy paste plain text in RichTextBox in C#
By FoxLearn 7/18/2024 7:47:15 AM 7.4K
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.
Categories
Popular Posts
Responsive Animated Login Form
11/11/2024
Carpatin Admin Dashboard Template
11/17/2024
Plus Admin Dashboard Template
11/18/2024