How to copy paste plain text in RichTextBox in C#

By FoxLearn 7/18/2024 7:47:15 AM   7.57K
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.

c# copy paste in richtextbox

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.