简体   繁体   中英

C# How can I paste formatted text from clipboard to the RichTextBox

I added context menu to the richboxtext with only one function "paste". What code will paste my clipboard content (eg copied from Microsoft Word) to the richboxtext form? I tried with:

    private void PasteToolStripMenuItem_Click_1(object sender, EventArgs e)
    {
        richTextBox1.Text = Clipboard.GetText();
    }

but it pastes non-formatted text. How can I paste text with the formatting?

DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Html);

if(richTextBox1.CanPaste(myFormat))
{
    richTextBox1.Paste(myFormat);
    return true;
}

you should change the Dataformats.Html of which type your Richtextbox should allow.

Here's the list of DataFormats : http://msdn.microsoft.com/en-us/library/system.windows.forms.dataformats.aspx

Got it!

Just specificy the format:

richTextBox1.Text = Clipboard.GetText(TextDataFormat.Rtf);

UPDATE

This will help you get formatted text(text only) from MS Word

尝试:

richTextBox1.selectedRtf=Clipboard.GetData(DataFormats.Rtf).ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM