转换卡口插入空格一个RichTextBox [英] Converting tabs into spaces in a RichTextBox
问题描述
我有一个表单上的的RichTextBox
控制一个WinForms应用程序。现在,我有 AcceptsTabs
属性设置为true,这样当<大骨节病>标签骨节病>被击中,它插入一个制表符。
I have a WinForms application with a RichTextBox
control on the form. Right now, I have the AcceptsTabs
property set to true so that when Tab is hit, it inserts a tab character.
我想,虽然做的是让这个当<大骨节病>标签骨节病>被击中,4位被插入一个而不是\t
制表符(我使用的是等宽字体)。我怎样才能做到这一点?
What I would like to do though is make it so that when Tab is hit, 4 spaces are inserted instead of a \t
tab character (I'm using a monospaced font). How can I do this?
推荐答案
添加一个新的类重写你的的RichTextBox
:
Add a new class to override your RichTextBox
:
class MyRichTextBox : RichTextBox
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData == Keys.Tab)
{
SelectionLength = 0;
SelectedText = new string(' ', 4);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
您可以然后拖动到设计新的控制鉴于您的形式为:
You can then drag your new control on to the Design view of your form:
注意:不像@ LarsTec的回答,设置 AcceptsTab
这里不需要
Note: unlike with @LarsTec's answer, setting AcceptsTab
is not required here.
这篇关于转换卡口插入空格一个RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!