Modifying default tab size in RichTextBox

后端 未结 5 767
北海茫月
北海茫月 2020-12-11 01:07

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.

5条回答
  •  不知归路
    2020-12-11 01:33

    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:

    • AcceptsTab = True TabSize
    • ConvertTabToSpaces = True
    • TabSize = 4

    Code

    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace MyNamespace
    {
        public partial class MyRichTextBox : RichTextBox
        {
            public MyRichTextBox() : base() =>
                KeyDown += new KeyEventHandler(RichTextBox_KeyDown);
    
            [Browsable(true), Category("Settings"), Description("Convert all tabs into spaces."), EditorBrowsable(EditorBrowsableState.Always), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            public bool ConvertTabToSpaces { get; set; } = false;
    
            [Browsable(true), Category("Settings"), Description("The number os spaces used for replacing a tab character."), EditorBrowsable(EditorBrowsableState.Always), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            public int TabSize { get; set; } = 4;
    
            [Browsable(true), Category("Settings"), Description("The text associated with the control."), Bindable(true), EditorBrowsable(EditorBrowsableState.Always), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
            public new string Text
            {
                get => base.Text;
                set => base.Text = ConvertTabToSpaces ? value.Replace("\t", new string(' ', TabSize)) : value;
            }
    
            protected override bool ProcessCmdKey(ref Message Msg, Keys KeyData)
            {
                const int WM_KEYDOWN = 0x100; // https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-keydown
                const int WM_SYSKEYDOWN = 0x104; // https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-syskeydown
    
                if (ConvertTabToSpaces && KeyData == Keys.Tab && (Msg.Msg == WM_KEYDOWN || Msg.Msg == WM_SYSKEYDOWN))
                {
                    SelectedText += new string(' ', TabSize);
                    return true;
                }
                return base.ProcessCmdKey(ref Msg, KeyData);
            }
    
            public new void AppendText(string text)
            {
                if (ConvertTabToSpaces)
                    text = text.Replace("\t", new string(' ', TabSize));
                base.AppendText(text);
            }
    
            private void RichTextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V))
                {
                    SuspendLayout();
                    int start = SelectionStart;
                    string end = Text.Substring(start);
                    Text = Text.Substring(0, start);
                    Text += (string)Clipboard.GetData("Text") + end;
                    SelectionStart = TextLength - end.Length;
                    ResumeLayout();
                    e.Handled = true;
                }
            }
    
        } // class
    } // namespace
    

提交回复
热议问题