richTextBox - 添加文本和表格 [英] richTextBox - add text and table

查看:297
本文介绍了richTextBox - 添加文本和表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将格式化的文本和表格添加到 RichTextBox.

i want to add formatted text and table to a richTextBox.

为此,我使用这些代码:

For this I use these codes:

文字:

richTextBox1.SelectionFont = new Font("Maiandra GD", 30, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionIndent = 0;
richTextBox1.AppendText("text text text");

还有桌子:

StringBuilder tableRtf = new StringBuilder();

tableRtf.Append(@"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}");
for (int j = 0; j <5; j++)
{
    tableRtf.Append(@"\trowd");
    tableRtf.Append(@"\cellx2500" + "  ghhghgjghjghjhggjh");
    tableRtf.Append(@"\intbl\cell");
    tableRtf.Append(@"\cellx10000\intbl\cel");
    tableRtf.Append("   " + "sdfsdfs" + @"\intbl\clmrg\cell\row");
}

tableRtf.Append(@"\pard");
tableRtf.Append(@"}");
richTextBox1.Rtf=tableRtf.ToString();

但是

richTextBox1.Rtf=tableRtf.ToString();

杀死之前的内容.

如何让它们兼容?

它不是重复的,因为我想要两件事:

It is not a duplicate because I want two thing:

1) 以这种方式将格式化文本添加到richTextBox:

1) add formatted text to richTextBox this way:

richTextBox1.SelectionFont = new Font("Maiandra GD", 30, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText("text text text");

可读性很好,我可以轻松修改.

It is well readable and I can modify easily.

2) 我想添加表格.

所以结构:

text text text text text文字文字文字文字文字

text text text text text text text text text text

|表格|

text text text text text文字文字文字文字文字文字文字文字文字文字文字

text text text text text text text text text text text text text text text

|表格|

但我不知道如何在不丢失以前内容的情况下应用表格?

But I don't know how can I apply tables without losing previous contents?

推荐答案

您需要做的是将 rtf 代码解析为标题和正文.

What you need to do is to dissect the rtf codes into the headers and the bodies.

表体从循环开始,保留 \par 肯定是个好主意.

The table body starts with the loop and keeping the \par is surely a good idea.

但是您既不能替换旧文本,也不能简单地将正文附加到末尾.

But you must neither replace the old text nor simply append the body to the end.

相反,您需要将它插入在最后一个卷曲之前!这是因为最后一个卷曲标志着整个 rtf 代码的结束,它之后的任何内容都将被忽略!

Instead you need to insert it before the last curly! This is because that last curly marks the end of the whole rtf code and anything after it will be ignored!

这很简单.

对于完整的解决方案,您还需要合并标题.

For a full solution you also will want to combine the headers.

这需要做更多的工作,将其全部写出来将超出 SO 答案.

This is a lot more work and writing it all out would go beyond an SO answer.

但基本原理很简单:

您需要了解您的表格标题添加到原始标题中已有内容的内容.

You need to understand the things your table header adds to the things already in the primal header.

最常见的是字体表颜色表.

所以如果你想在附表中使用一种或多种不同的字体,你需要这样做:

So if you want to use one or more different fonts in the appended table you need to do this:

  • 使用新的字体索引将它们添加到字体表中,例如作为 \f1\fnil Consolas; 在前一个分号之后.
  • 通过更改循环以在第一个 \intbl 表格段落格式控制字之后包含新字体来使用它:\intbl\f2\fs24 ghhghgjghjghjhggjh.
  • 如果您想在表格中使用不同的字体,请根据需要重复.
  • 添加 cfN 字体颜色选择器(如果需要).
  • add them to the font table with a new font index, e.g. as \f1\fnil Consolas; after the previous semicolon.
  • use it by changing the loop to include the new font right after the first \intbl table-paragraph-formatting control word: \intbl\f2\fs24 ghhghgjghjghjhggjh.
  • repeat as needed if you want to use different fonts in the table.
  • add a cfNfont color selector, if you want to.

同样的想法也适用于颜色表.它没有明确的索引,所以顺序很重要;附加所有颜色,每个颜色末尾都有一个分号:

The same idea will also work for the color table. It doesn't have a explicit indexing, so order matters; all colors are appended, each with a semicolon at the end:

{\colortbl ;\red255\green0\blue0;\red25\green0\blue220;}

.. 为格式化文本中的红色添加蓝色.

..adds a blue color to the red from the formatted text.

你看,这是工作,需要一些努力和准备.

You see, this is work and takes some effort and preparations.

您可以在此处找到完整的 rtf 规格.

You can find the full rtf specs here.

这里是使用 rtf.. 玩一点的截图:

Here is a screenshot of playing a little with the rtf..:

注意表头部分是由控件创建的;您可能想为此使用虚拟控件,或者您可以弄清楚哪些部分是您需要的,哪些是不需要的..

Note that the parts of table header was created by the control; you may want to use a dummy control for this or maybe you can figure out which parts you need and which are not necessary..

更新:这是一个附加 rtf for dummys"版本:

Update: Here is a 'appending rtf for dummies' version:

tableRtf.Append(@"{\fonttbl{\f0\fnil\fcharset0 Courier;}}");
for (int j = 0; j <5; j++)
{
    tableRtf.Append(@"\trowd");
    tableRtf.Append(@"\cellx2500" + "  ghhghgjghjghjhggjh");
    tableRtf.Append(@"\intbl\cell");
    tableRtf.Append(@"\cellx10000\intbl\cel");
    tableRtf.Append("   " + "sdfsdfs" + @"\intbl\clmrg\cell\row");
}

tableRtf.Append(@"\pard");
tableRtf.Append(@"}");

string rtf1 = richTextBox1.Rtf.Trim().TrimEnd('}');
string rtf2 = tableRtf.ToString();
richTextBox1.Rtf  = rtf1 + rtf2;

请注意,在表格正文之前插入的字体表格确实有效!但是一定不要添加rtf-start标签!!

Note that the font table inserted before the table body does work! But make sure not to add the rtf-start tag!!

这篇关于richTextBox - 添加文本和表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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