简体   繁体   English

richTextBox - 添加文本和表格

[英]richTextBox - add text and table

i want to add formatted text and table to a richTextBox.我想将格式化的文本和表格添加到 richTextBox。

For this I use these codes:为此,我使用这些代码:

Text:文本:

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

And the table:和表:

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();

But the但是

richTextBox1.Rtf=tableRtf.ToString();

kills the previous contents.杀死以前的内容。

How can I make compatible them?我怎样才能使它们兼容?


It is not a duplicate because I want two thing:它不是重复的,因为我想要两件事:

1) add formatted text to richTextBox this way: 1)以这种方式将格式化文本添加到richTextBox:

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) And I want to add tables. 2)我想添加表格。

So the structure:所以结构:

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

|TABLE| |表|

text text text text text text text text text text text text text text text正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文

|TABLE| |表|

etc.等等

But I don't know how can I apply tables without losing previous contents?但是我不知道如何在不丢失以前内容的情况下应用表格?

What you need to do is to dissect the rtf codes into the headers and the bodies.您需要做的是将 rtf 代码分解为标题和正文。

The table body starts with the loop and keeping the \par is surely a good idea.表体以循环开始,保留\par肯定是个好主意。

But you must neither replace the old text nor simply append the body to the end.但是您既不能替换旧文本,也不能简单地将正文附加到末尾。

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!这是因为最后一个卷曲标记了整个 rtf 代码的结尾,之后的任何内容都将被忽略!

This was simple.这很简单。

For a full solution you also will want to combine the headers.对于完整的解决方案,您还需要组合标题。

This is a lot more work and writing it all out would go beyond an SO answer.这是更多的工作,将它全部写出来将超出 SO 答案。

But the basic principle is simple:但基本原理很简单:

You need to understand the things your table header adds to the things already in the primal header.您需要了解表头添加到原始表头中已有内容的内容。

The most common things are a font table and a color table .最常见的是font tablecolor table

So if you want to use one or more different fonts in the appended table you need to do this:所以如果你想在附表中使用一种或多种不同的字体,你需要这样做:

  • add them to the font table with a new font index, eg as \f1\fnil Consolas;使用新的字体索引将它们添加到字体表中,例如\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 .通过更改循环以在第一个\intbl table-paragraph-formatting 控制字之后包含新字体来使用它: \intbl\f2\fs24 ghhghgjghjghjhggjh
  • repeat as needed if you want to use different fonts in the table.如果您想在表格中使用不同的字体,请根据需要重复。
  • add a cfN font color selector, if you want to.如果需要,添加cfN字体颜色选择器。

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.你看,这是工作,需要一些努力和准备。

You can find the full rtf specs here.您可以在此处找到完整的 rtf 规范。

Here is a screenshot of playing a little with the rtf..:这是使用 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..你可能想为此使用一个虚拟控件,或者你可以弄清楚哪些部分是你需要的,哪些不是必需的。

Update: Here is a 'appending rtf for dummies' version:更新:这是一个“附加 rtf for dummies”版本:

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;

Note that the font table inserted before the table body does work!请注意,在表体之前插入的字体表确实有效! But make sure not to add the rtf-start tag!!但请确保不要添加 rtf-start 标签!!

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM