views:

2090

answers:

3

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.

+6  A: 

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";
Scott Nichols
A: 

Followup question:

Does setting SelectionTabs alter the RTF output from the control so that it will appear the same way in other RTF viewing tools?

Jeff Kotula
A: 

I can confirm the answer by Scott Nichols works correctly. I'd vote it up but it appears that stack overflow is too stupid to allow a new user (that's gone to the trouble of signing up for open id etc) to do this.

I can also answer the follow up question by Jeff Kotula. The SelectionTabs property only applies to the control. It does not alter the RTF itself and will not affect how that text appears in other RTF viewing tools.

Ben Robbins