RichTextBox-即使粘贴后仍保留原始格式(字体) [英] RichTextBox - retain original formatting (font), even after paste

查看:64
本文介绍了RichTextBox-即使粘贴后仍保留原始格式(字体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用RichTextBox,而不是普通的文本框,因为它可以保持插入符号位置(逐行). 但是我需要始终将文本保持相同的字体,即使粘贴也是如此.

I need to use a RichTextBox, not a normal textbox because of the way it keeps the caret position, from line to line. But I need to keep the text in the same font all the time even if it is pasted.

目前,我已经选择了整个文本并将字体更改为原始字体(Lucida控制台),但是将其粘贴到它时会显得很恐怖,因为它会闪烁蓝色.

At the moment I have it selecting the entire text and changing the font to the original (Lucida Console) but it look horrible when you paste into it as it flashes blue.

推荐答案

如果您以编程方式处理粘贴,请不要使用

If you are handling the pasting programatically don't use the Paste method. Instead use Clipboard.GetDataObject().GetData(DataFormats.Text) to get the text in a string and then add the text using the Rtf or Text property to the RichTextBox:

string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
richTextBox.Text += s;

否则,您可以按 Ctrl + V 键:

void RichTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Control == true && e.KeyCode == Keys.V)
    {
        string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
        richTextBox.Text += s;
        e.Handled = true; // disable Ctrl+V
    }
}

这篇关于RichTextBox-即使粘贴后仍保留原始格式(字体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆