Winforms RichTextBox 中的两列 [英] Two columns in Winforms RichTextBox

查看:53
本文介绍了Winforms RichTextBox 中的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,我需要能够在单个 RichTextBox 中至少给出 2 列的印象.就我而言,它可能如下所示:

In C#, I need to be able to at least give the impression of 2 columns in a single RichTextBox. For my purposes, it could look like this:

1+1             2
70*8+5          565
1000000-300000  700000
76-10           66

对于左栏中的每个问题",都会有相应的答案.

For each 'question' in the left column, there will be a corresponding answer.

显而易见"的解决方案是使用这样的标签:

The 'obvious' solution is to use tabs like this:

SelectionTabs = new int[] { 0, 500 };

(或简单地将原始 Rtf 中的 \deftab 设置为大约 3000).

(or simply set \deftab to about 3000 in the raw Rtf).

这一切都很好,直到左列输入 ITSELF 包含选项卡.在这种情况下,一切都会变成梨形.我不介意插入多个制表位,但是当输入包含有时经过"特定制表位的文本时,这将无法正常工作.检测到这一点并正确解释它不仅很难做到,而且由于打印时出现的几乎但不完全是所见即所得"的结果以及选项卡受到的影响,也充满了危险.

This is all well and good, until the left column input ITSELF contains tabs. When this is the case, everything goes pear-shaped. I don't mind inserting multiple tab-stops, but this won't work well when the input contains text which sometimes goes 'past' a particular tab-stop. Detecting this and accounting for it properly is not just hard to do, but also fraught with peril due to the 'very-almost-but-not-quite-WYSIWIG' results that occur when printing, and how tabs are affected.

另一个想法是有一个全局制表符大小(在 Rtf 中使用 \deftab),然后计算在左列中的每一行之后应该自动插入多少个制表符(基于左列中最长的行).这种方法的问题在于,虽然屏幕上的东西看起来很棒,但在打印时,右列有一些小故障.通过故障",我的意思是每 20 行中大约有 1 行会有丢失"或额外"选项卡.这是因为打印并不完全是所见即所得,即使我遵循了本教程:http://msdn.microsoft.com/en-us/library/ms996492.aspx

Another idea is to have a global tab size (using \deftab in the Rtf), and then calculate how many tabs should automatically be inserted after each line in the left column (based on the longest line in the left column). The problem with this approach is that while things look great on the screen, when it comes to printing, the right column has some glitches. By 'glitches', I mean approximately 1 in every 20 lines will have a 'lost' or 'extra' tab. This is because printing isn't entirely WYSIWIG, even after I followed this tutorial: http://msdn.microsoft.com/en-us/library/ms996492.aspx

可以说,与 RichTextBox 在屏幕上的视图相比,即使打印中的几个像素/点也会对选项卡的位置产生巨大影响.

Suffice to say, even a few pixels/points off in print can have a drastic effect on the positioning of the tabs in comparison to the RichTextBox's view on the screen.

我还尝试在 Rtf 中的一行中更改制表位的位置,但不幸的是,尽管它在 Rtf 中的位置,但从行的开头应用了这样的控制代码.

I've also tried changing the tabstop position halfway through a line in the Rtf, but unfortunately, such a control code is applied from the beginning of the line, despite its location in the Rtf.

我还结合了 http://www.biblioscape.com/rtf15_spec 中的 Rtf 规范.htm,而且我似乎无能为力.C# 的 RichTextBoxes 似乎不能很好地处理表格(这是另一个潜在的解决方案).还有 Rtf 列,但即使支持它们,它们也可能无济于事,因为第一列在填充后会切换到同一张纸上的第二列(而不是保持在第一列和转到第二页纸,这正是我想要的).

I've also been combing the Rtf specification from http://www.biblioscape.com/rtf15_spec.htm, and there doesn't seem to be much I can do about it. C#'s RichTextBoxes don't seem to get on well with tables (which was another potential solution). There's also Rtf columns, but even if they are supported, they probably won't help due to the way the first column will switch to the second column on the same sheet of paper once it's been filled (instead of keeping to the first column and going to the second page of paper which is what I would want).

我已经给了这个问题很多时间,我完全不知所措.如此简单的事情实际上是非常棘手的.除非我遗漏了一些明显的东西......

I've given this problem so much time, and I'm at a complete loss. Something so simple is actually incredibly tricky to do properly. Unless I'm missing something obvious...

---------------- 编辑 1:这需要使用非固定宽度的字体,所以我不能只用空格填充.

---------------- EDIT 1: This needs to work with non fixed-width fonts, so I can't just pad with spaces.

推荐答案

诀窍很简单:我最近做了同样的事情(嗯,差不多)——我必须创建一个文本文件这给人的印象是它有多个列(在文本文件中?sheesh)

The trick is easy: I've recently done the same thing (well, almost) - I had to create a text file that gives the impression that it has multiple columns (in a text file? sheesh)

有什么诀窍?填充!

假设您拥有之前所述的值

Assume you have the values you stated earlier

1+1             2
70*8+5          565
1000000-300000  700000
76-10           66

现在,您需要做的就是设置左列的宽度(以字符为单位),以及设置左列的宽度右列.假设两个宽度都是 30 个字符.

Now, all you need to do is to set the width (in characters) of the left column, and the width for the right column. let's say both width's are 30 characters.

您现在要做的就是按照以下伪代码构造一行:

all you have to do now, is to construct a row as in the following pseudo code:

BEGIN-BUILD-TEXT
     FOR EACH PAIR X, Y
         STRING S1 = X.PADRIGHT(30);
         STRING S2 = Y.PADRIGHT(30);
         WRITE_LINE_TO_TEXTBOX(S1 + S2);

这样,列将左对齐并具有固定宽度.

This way, the columns will be left aligned and with a fixed width.

为我工作!

您的 RichTextBox 应该使用固定宽度的字体(它对所有字符具有相同的宽度).

Your RichTextBox should use a fixed-width font (it has the same width for all characters).

这篇关于Winforms RichTextBox 中的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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