c# RTB - 粘贴没有颜色/字体的纯文本? [英] c# RTB - paste plain text without colours/fonts?

查看:35
本文介绍了c# RTB - 粘贴没有颜色/字体的纯文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 C# 应用程序中使用富文本对象.我遇到的唯一问题是,当用户从另一个应用程序粘贴格式化文本时,它仍然是我无法拥有的格式化文本.有什么方法可以只粘贴字符串并忽略格式吗?谢谢!

I am using Rich Text object in my C# application. The only issue I am having is that when user pastes formated text from another app, it remains formated which I cannot have. Is there any way how to paste only string and ignore formatting? Thanks!

推荐答案

假设 WinForms :试试这个:定义一个带有 KeyDown 事件处理程序的 RichTextBox :

Assuming WinForms : try this : define a RichTextBox with a KeyDown event handler like this :

仅附加示例:

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V) 
        {
            richTextBox1.Text += (string)Clipboard.GetData("Text"); 
            e.Handled = true; 
        }
    }

在当前插入点(选择开始)将剪贴板 RTF 添加到 RichTextBox 示例:

Add Clipboard RTF to RichTextBox at current insertion point (selection start) example :

private void richTextBox1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Control && e.KeyCode == Keys.V)  
    { 
            // suspend layout to avoid blinking
            richTextBox2.SuspendLayout();

            // get insertion point
            int insPt = richTextBox2.SelectionStart;

            // preserve text from after insertion pont to end of RTF content
            string postRTFContent = richTextBox2.Text.Substring(insPt);

            // remove the content after the insertion point
            richTextBox2.Text = richTextBox2.Text.Substring(0, insPt);

            // add the clipboard content and then the preserved postRTF content
            richTextBox2.Text += (string)Clipboard.GetData("Text") + postRTFContent;

            // adjust the insertion point to just after the inserted text
            richTextBox2.SelectionStart = richTextBox2.TextLength - postRTFContent.Length;

            // restore layout
            richTextBox2.ResumeLayout();

            // cancel the paste
            e.Handled = true;
    } 
} 

[结束编辑]

注意 0:粘贴的文本 将假定当前对 RichTextBox 生效的样式设置:如果您将前景色"设置为蓝色":粘贴的文本将是蓝色.

Note 0 : The pasted in text is going to assume the current style settings in effect for the RichTextBox : if you have 'ForeGround color set to 'Blue : the pasted in text is going to be in blue.

注 1:这是我快速拼凑起来的东西,并通过使用写字板为剪贴板创建一些多色且格式怪异的 RTF 仅测试了几次:然后在运行时粘贴到 RichTextBox1 中:它确实剥离了去掉所有颜色、缩进等

Note 1 : This is something I knocked together quickly, and tested only a few times by creating some multi-colored and weirdly formatted RTF for the clipboard using WordPad : then pasting into into the RichTextBox1 at run-time : it did strip away all the color, indenting, etc.

由于尚未完全测试,请谨慎使用.

注意 2:这显然不会处理插入"或通过上下文菜单粘贴"的情况.

Note 2 : This will not handle the case of 'Insert or 'Paste via Context Menu, obviously.

欢迎对这个答案的所有批评,如果它不符合标准",将立即将其删除.

Welcome all critiques of this answer, and will immediately take it down if it's not "on the mark."

这篇关于c# RTB - 粘贴没有颜色/字体的纯文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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