How do I cut, copy and paste with formatting?

c#, formatting, visual-studio, winforms

Solution

Try this two function:

COPY

    private void Copy()
    {
        Clipboard.SetText(richTextBox1.Rtf, TextDataFormat.Rtf);
    }

PASTE

    private void Paste()
    {
        if (Clipboard.ContainsText(TextDataFormat.Rtf))
        {
            richTextBox1.Rtf = Clipboard.GetText(TextDataFormat.Rtf);

        }
    }

Problem

I have a WinForms application with a richTextBox. I would like to be able to cut, copy and paste formatted text in my application. Currently, my code consists of: Cut all: ``` richTextBoxPrintCtrl1.Cut(); ``` Cut Selected: ``` Clipboard.SetText(richTextBoxPrintCtrl1.Text); richTextBoxPrintCtrl1.Text = ""; ``` Copy all: ``` richTextBoxPrintCtrl1.Copy(); ``` Copy Selected: ``` Clipboard.SetDataObject(richTextBoxPrintCtrl1.SelectedText); ``` Paste: ``` DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Text); richTextBoxPrintCtrl1.Paste(myFormat); ``` I would like it so that if I cut/copy text from the richTextBox, it keeps ALL formatting (size, font, colour etc.), and if I paste text to the richTextBox, it also keeps all formatting. How would this be achieved? Thanks.

Original source