Setting Tab Size in TextBox

  • Thread starter Thread starter mj2736
  • Start date Start date
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 });
}
 
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>
 
Back
Top