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

落爺英雄遲暮 提交于 2019-12-01 04:38:09

问题


I added context menu to the richboxtext with only one function "paste". What code will paste my clipboard content (e.g. 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?


回答1:


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




回答2:


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




回答3:


Try:

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


来源:https://stackoverflow.com/questions/9749141/c-sharp-how-can-i-paste-formatted-text-from-clipboard-to-the-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!