Set tab stop positions for a multiline TextBox control

By default, multiline TextBox controls insert a tab stop position every 8 characters; in other words, if you press the Ctrl+Tab key while the focus is inside a multiline TextBox control, the caret advances to position 8,16,24, etc. (If the TextBox is the only control on the form that can receive the focus in that moment, you can also advance to the next tab stop with the Tab key.)

By sending the control a EM_SETTABSTOPS message, you can set individual tab stops at desired positions. You do so by loading an array of Longs with the position of individual tab stops, expressed in dialog units – where each dialog unit is 1/4 of the average character width. Then you pass the array to the SendMessage function, specifying the number of elements in wParam and the first element of the array in lParam. Here’s an example:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _    lParam As Any) As LongConst EM_SETTABSTOPS = &HCB' Set tab stops at character position 5, 10, and 20Dim tabs(2) As Longtabs(0) = 5 * 4tabs(1) = 10 * 4tabs(2) = 20 * 4SendMessage Text1.hWnd, EM_SETTABSTOPS, 3, tabs(0)

The following routine encapsulates this call for you:

' Set tab stops. Each element of the array is expressed in dialog units,' where each dialog unit is 1/4 of the average character width.Sub TextBoxSetTabStops(tb As TextBox, tabStops() As Long)    Dim numEls As Long    numEls = UBound(tabStops) - LBound(tabStops) + 1    SendMessage tb.hwnd, EM_SETTABSTOPS, numEls, tabStops(LBound(tabStops))End Sub

The simplest case is when all the tab stops have the same distance, in which case you can use the following routine:

' Set the tab stop distance, expressed in dialog units.Sub TextBoxSetTabStopDistance(tb As TextBox, ByVal distance As Long)    SendMessage tb.hwnd, EM_SETTABSTOPS, 1, distanceEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS