Setting Tab Size in TextBox

M

mj2736

I need to write a function that will set the width of tabs in a
multiline textbox windows control to a specified number of characters.
Here is the code I've tried; it is changing the tab size, but not to
the size I'm expecting. Am I going about this the wrong way? Thanks.


private const int EM_SETTABSTOPS = 0x00CB;

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr h, int msg, int wParam,
int[] lParam);

public static void SetTabWidth(TextBox textbox, int tabWidth)
{
Graphics graphics = textbox.CreateGraphics();
int characterWidth = (int) graphics.MeasureString("M",
textbox.Font).Width;

SendMessage(textbox.Handle, EM_SETTABSTOPS, 1, new int[] { tabWidth
* characterWidth });
}
 
H

Herfried K. Wagner [MVP]

I need to write a function that will set the width of tabs in a
multiline textbox windows control to a specified number of characters.
Here is the code I've tried; it is changing the tab size, but not to
the size I'm expecting.
[...]
Graphics graphics = textbox.CreateGraphics();
int characterWidth = (int) graphics.MeasureString("M",
textbox.Font).Width;

SendMessage(textbox.Handle, EM_SETTABSTOPS, 1, new int[] { tabWidth
* characterWidth });
}

The values must be in Dialog Template Units, not pixels:

| A pointer to an array of unsigned integers specifying the tab
| stops, in dialog template units. If the 'wParam' parameter is
| 1, this parameter is a pointer to an unsigned integer containing
| the distance between all tab stops, in dialog template units

A VB6 sample demonstrating how to convert the width to DTUs can be found
here:

<URL:http://dotnet.mvps.org/vb/code/controls/#TextBoxTabStops>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top