Modifying default tab size in RichTextBox
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();
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
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";
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.