Given a WinForms TextBox control with MultiLine = true
and AcceptsTab == true
, how can I set the width of the tab character displayed?
I want to use this as a quick and dirty script input box for a plugin. It really doesn't need to be fancy at all, but it would be nice if tabs were not displayed as 8 characters wide...
I know you are using a
TextBox
currently, but if you can get away with using aRichTextBox
instead, then you can use the SelectedTabs property to set the desired tab width:Note that these offsets are pixels, not characters.
The example offered is incorrect.
The
EM_SETTABSTOPS
message expects the tab sizes to be specified in dialog template units and not in pixels. After some digging around, it appears that a dialog template unit equals to 1/4th the average width of the window's character. So you'll need to specify 8 for 2 characters long tabs, 16 for four charachters, and so on.So the code can be simplified as:
For anyone who wants different tab widths, I took this approach:
I think sending the
EM_SETTABSTOPS
message to the TextBox will work.This can be called in the constructor of your
Form
, but beware: Make sureInitializeComponents
is run first.this is very useful:
Set tab stop positions for a multiline TextBox control
With the use of extension methods, you can add a new method to the TextBox control class. This is my implementation (including an additional extension method that gives you the coordinates for the current location of the insert caret) from what I gathered from the previous contributors above: