修改 RichTextBox 中的默认选项卡大小
有没有办法更改 .NET RichTextBox 中的默认选项卡大小? 目前似乎设置为相当于 8 个空间,这对我来说有点大。
编辑:为了澄清,我想将“\t”的全局默认设置设置为控件的 4 个空格。 据我所知,SelectionTabs 属性要求您首先选择所有文本,然后通过数组选择选项卡宽度。 如果必须的话我会这样做,但如果可能的话,我宁愿只更改全局默认值一次,这样我就不必每次都这样做。
Is there any way to change the default tab size in a .NET RichTextBox?
It currently seems to be set to the equivalent of 8 spaces which is kinda large for my taste.
Edit: To clarify, I want to set the global default of "\t" displays as 4 spaces for the control. From what I can understand, the SelectionTabs property requires you to select all the text first and then the the tab widths via the array. I will do this if I have to, but I would rather just change the global default once, if possible, sot that I don't have to do that every time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将此类与等宽字体一起使用; 它将所有制表符替换为空格。
您所要做的就是根据您的要求设置以下设计器属性:
TabSize
PS:正如@ToolmakerSteve指出的,显然这里的制表符大小逻辑非常简单:它只是将制表符替换为4个空格,这只适用于开头的制表符每行。 如果您需要改进选项卡处理,只需扩展逻辑即可。
代码
I'm using this class with monospaced fonts; it replaces all TABs with spaces.
All you have to do is to set the following designer properties according to your requirements:
TabSize
PS: As @ToolmakerSteve pointed out, obviously the tab size logic here is very simple: it just replaces tabs with 4 spaces, which only works well for tabs at the beginning of each line. Just extend the logic if you need improved tab treatment.
Code
您可以通过设置 SelectionTabs 财产。
更新:
顺序很重要......
如果您在初始化控件的文本之前设置选项卡,则不必在设置选项卡之前选择文本。
例如,在上面的代码中,这将保留带有原始 8 个空格制表位的文本:
但这将使用新的:
You can set it by setting the SelectionTabs property.
UPDATE:
The sequence matters....
If you set the tabs prior to the control's text being initialized, then you don't have to select the text prior to setting the tabs.
For example, in the above code, this will keep the text with the original 8 spaces tab stops:
But this will use the new ones:
Winforms 没有用单个数字设置 RichTexBox 的默认制表符大小的属性,但如果您准备深入研究富文本框的 Rtf 并对其进行修改,则可以使用一个设置,称为: “\deftab”。 后面的数字表示缇数(1 点 = 1/72 英寸 = 20 缇)。 标准选项卡大小为 720 缇的生成的 Rtf 可能类似于:
如果您需要将缇转换为像素,请使用受 将像素转换为点:
Winforms doesn't have a property to set the default tab size of a RichTexBox with a single number, but if you're prepared to dig into the Rtf of the rich text box, and modify that, there's a setting you can use called: "\deftab". The number afterwards indicates the number of twips (1 point = 1/72 inch = 20 twips). The resulting Rtf with the standard tab size of 720 twips could look something like:
If you need to convert twips into pixels, use this code inspired from Convert Pixels to Points:
奇怪的是,一直没有人提出这个方法)
我们可以继承
RichTextBox
并重写CmdKey处理程序(ProcessCmdKey)它看起来像这样:
现在,当您按Tab,控制区域将插入指定数量的空格,而不是
\t
It's strange that no one has proposed this method for all this time)
We can inherit from the
RichTextBox
and rewrite the CmdKey handler (ProcessCmdKey)It will look like this:
Now, when you'll press Tab, a specified number of spaces will be inserted into the control area instead of
\t
如果您有一个仅用于显示(只读)固定间距文本的 RTF 框,那么最简单的事情就是不要搞乱制表位。 只需用空格替换它们即可。
如果您希望用户可以输入某些内容并使用 Tab 键前进,您还可以通过覆盖
OnKeyDown()
来捕获 Tab 键并打印空格。If you have a RTF box that is only used to display (read only) fixed pitch text, the easiest thing would be not to mess around with Tab stops. Simply replace them stuff with spaces.
If you want that the user can enter something and use that Tab key to advance you could also capture the Tab key by overriding
OnKeyDown()
and print spaces instead.