Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying default tab size in RichTextBox

Tags:

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.

like image 813
Adam Haile Avatar asked Sep 30 '08 17:09

Adam Haile


3 Answers

You can set it by setting the SelectionTabs property.

private void Form1_Load(object sender, EventArgs e)
{
    richTextBox1.SelectionTabs = new int[] { 100, 200, 300, 400 };
}

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:

richTextBox1.Text = "\t1\t2\t3\t4";
richTextBox1.SelectionTabs = new int[] { 100, 200, 300, 400 };

But this will use the new ones:

richTextBox1.SelectionTabs = new int[] { 100, 200, 300, 400 };
richTextBox1.Text = "\t1\t2\t3\t4";
like image 199
Scott Nichols Avatar answered Oct 14 '22 10:10

Scott Nichols


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:

{\rtf1\ansi\ansicpg1252\deff0\deflang2057\deftab720{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs41
1\tab 2\tab 3\tab 4\tab 5\par
}

If you need to convert twips into pixels, use this code inspired from Convert Pixels to Points:

int tabSize=720;
Graphics g = this.CreateGraphics();
int pixels = (int)Math.Round(((double)tabSize) / 1440.0 * g.DpiX);
g.Dispose();
like image 38
Dan W Avatar answered Oct 14 '22 11:10

Dan W


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:

public class TabRichTextBox : RichTextBox
{
    [Browsable(true), Category("Settings")]
    public int TabSize { get; set; } = 4;

    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
        // Tab has been pressed
        if ((Msg.Msg == WM_KEYDOWN || Msg.Msg == WM_SYSKEYDOWN) && KeyData.HasFlag(Keys.Tab))
        {
            // Let's create a string of spaces, which length == TabSize
            // And then assign it to the current position
            SelectedText += new string(' ', TabSize);

            // Tab processed
            return true;
        }
        return base.ProcessCmdKey(ref Msg, KeyData);
    }
}

Now, when you'll press Tab, a specified number of spaces will be inserted into the control area instead of \t

like image 28
Kir_Antipov Avatar answered Oct 14 '22 10:10

Kir_Antipov