C# 删除富文本框中的电子表格格式

C# 删除富文本框中的电子表格格式,c#,winforms,C#,Winforms,我想将MS Excel中的一些行复制并粘贴到我的C#Winforms应用程序中的richTextBox中。用户将在键盘上按CTRL+V,Excel网格线显示出来。如何确保粘贴的内容仅显示为文本# 这似乎不起作用: private void richTextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Control == true && e.KeyCode == Keys.V) { e.Ha

我想将MS Excel中的一些行复制并粘贴到我的C#Winforms应用程序中的richTextBox中。用户将在键盘上按CTRL+V,Excel网格线显示出来。如何确保粘贴的内容仅显示为文本#

这似乎不起作用:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control == true && e.KeyCode == Keys.V)
    {
        e.Handled = true;
        string st = Clipboard.GetText();
        richTextBox1.Text = st;
    }
}
我无法使用文本框,因为我的代码如下所示:

private void button1_Click(object sender, EventArgs e)
{
    richTextBox2.Clear();
    richTextBox2.Focus();

    string strValues;
    strValues = richTextBox1.Text;

    var textInEachLine = richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
    string whereClause = string.Join("', '", textInEachLine).ToString();
    richTextBox2.AppendText(" IN ( '" + whereClause + "')");
}
试试这个代码

 private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
            {
                e.Handled = true;
                string st = Clipboard.GetText();
                richTextBox1.Text = st;
            }
            base.OnKeyDown(e);
        }



 private void richTextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.Control == true && e.KeyCode == Keys.V) { e.IsInputKey = true; }
    }

一个更好的解决方案是将richTextBox转换为多行文本框。

我四处寻找更好的答案已经有一段时间了。我已经想出了自己的解决方案,因为我无法摆脱我支持的应用程序中的RichTextBox。我编写了一个表单应用程序来查看原始RTF数据,并从excel中粘贴了一些单元格。 空白文本框产生…

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil Calibri;}} \viewkind4\uc1\pard\f0\fs22\par }

当粘贴到一些带有彩色文本的单元格中时,它会显示

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil Calibri;}} {\colortbl;\red255\green0\blue0;\red0\green0\blue0;} \viewkind4\uc1\trowd\trgaph30\trleft-30\cellx1002\cellx2034\pard\intbl\cf1\f0\fs22好文本\cell\cf2好文本\cell\row \intbl好文本\cell\cf1好文本\cell\row \pard\cf0\par }

我发现删除“\cell”或\cellx######”并将文本返回到另一个RichTextBox会导致删除所有相关格式。RichTextBox似乎会删除无效的格式。 有了这个,我编写了以下方法,在将文本粘贴到RichTextBox之前执行

private void CleanClipboardText()
    {
        string cleaned = Clipboard.GetText(TextDataFormat.Rtf);
        if (cleaned != null & cleaned != String.Empty)
        {
            Regex regex = new Regex(@"\\cellx\d{4}?");
            cleaned = regex.Replace(cleaned, " ");
            regex = new Regex(@"\\cell");
            cleaned = regex.Replace(cleaned, " ");
            Clipboard.SetText(cleaned, TextDataFormat.Rtf);
        }
    }
此事件用于“按键”事件


我使用了
.Paste()因为我不想将整个框的文本覆盖为
richTextBox1.text=st将。但是,如果您愿意,您可能希望CleanCippboardText方法返回一个rtf数据字符串,然后改为设置
RichTextBox.rtf

一个解决方案可能是使用
textbox
multiline
选项设置为true,而不是
RichTextBox
用我的代码更新我的问题。我不能使用文本框,因为我使用的是字符串数组。除非您可以重写它,并向我展示您提供的代码正在工作,否则问题肯定出在其他地方。此
richtextbox
上还有其他事件吗?当您说它工作时,您是否从Excel复制单元格并尝试粘贴它?如果你看到网格线,那么它就不起作用了……相信我,我没有看到任何网格线。所以这个richtextbox上没有其他事件了,对吗?不起作用。。。我仍然从excel中获得粘贴的网格
private void RTB_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            RichTextBox rtb = sender as RichTextBox;
            e.Handled = true;
            CleanClipboardText();
            rtb.Paste();
        }
    }