richTextBox - add text and table

元气小坏坏 提交于 2020-01-14 14:35:12

问题


i want to add formatted text and table to a 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:

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.

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?


回答1:


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

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.

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!

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.

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 afont table and a color 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, 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.

You can find the full rtf specs here.

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..

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;

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



来源:https://stackoverflow.com/questions/45104890/richtextbox-add-text-and-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!