Modifying default tab size in RichTextBox

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.

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";

Modifying default tab size in RichTextBox, 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  By default, the Full rich text (images, tables, etc.) check box is selected in the Rich Text Box Properties dialog box so that users can insert pictures and tables inside the rich text box. You can specify that users can embed images in the form, which saves the actual image data in the underlying form (.xml) file, or link images, which saves a

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();

https://social.msdn.microsoft.com/Forums/en-US/c98, With the RichTextBox control, the user can enter and edit text. Gets or sets the default action description of the control for use by Gets or sets the indentation used in the RichTextBox control when the bullet style is applied to the text. CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100

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.

RichTextBox Class (System.Windows.Forms), After you insert a rich text box on a form template, you can customize it by For example, you may be able to change the size of the control but not its field or The default tab index setting for all controls in a form template is 0, but the tab order  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).

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

Ways to customize a rich text box - InfoPath - office support, This causes the tab to expand in size. Then there is the RichTextBox. Controls. You've You can modify the way the user interacts with the TextBox as well. The code sets AcceptsTab to true so you can type tab characters into the control at run time without moving focus to the next control. This entry was posted in controls and tagged C# , C# programming , controls , example , example program , Rich Text box , RichTextBox , RichTextBox tab stops , RichTextBox tabs , RTF , set tabs , set tabs stops

Sams Teach Yourself .NET Windows Forms in 21 Days, The WPF RichTextBox can be set to accept tabs in its input. It renders the result as a 4 character indent. I'd like to change that size. How can I change the default​  This example shows how to programmatically change the current selection in a RichTextBox. This selection is the same as if the user had selected the content by using the user interface. Example. The following Extensible Application Markup Language (XAML) code describes a named RichTextBox control with simple content.

How do I set tab size in the WPF RichTextBox - .net - iOS, A RichTextBox is a better option when it is necessary for the user to edit You can also set the PageWidth property of the FlowDocument to be larger than the width of the RichTextBox . Scrollbars are not visible in the RichTextBox by default . SHIFT + TAB TabForward ТАВ បាញយយយយយយាអោយបងចាមមហើយ Table 11 . Click the Home tab and then click the Paragraph group's dialog launcher (the small arrow in the bottom-right corner). In Word 2003, choose Tab from the Format menu. Click Tabs (in the bottom-left

C# 2008 Programming: Covers .Net 3.5 Black Book, Platinum Ed, The rich text box does not have a setting but RTF does have tags for this in version 1.5. An RTF file is a tagged format created MANY years ago to work with 7 bit ASCII and * bit mac computers. The TAGS are all written in ASCII so you can open the file and insert the settings that you want to have in the file with your favorite method of

Changing the zoomed size will not affect the true size (printed size) of the graph. Modify a graph's size To modify the true size or zoomed size of a graph, double-click the graph region or figure region then click the Graph Size tab.

Comments
  • It might be useful to add that these values are the tab stop in pixels rather than in characters. It is mentioned on the MSDN page but seems somewhat counter-intuitive.
  • Unless you have 14pt spaces, bold spaces or otherwise formatted spaces.
  • You are right. I forgot to mention that this makes mainly sense for code editors that use 'Courier New' and only one font size. In 'Courier New' a bold space has the same width a normal space. But if you are using variable pitch fonts or different font sizes it would not be a good idea to replace tabs with spaces.
  • Tabs are used for distances. Your "spaces" would only half-way work if the end user is using a mono-spaced font.
  • This worked perfectly. One small issue. I had to change the KeyData.HasFlag(Keys.Tab) to KeyData == Keys.Tab