给定一个WinForms TextBox控件,MultiLine = true和AcceptsTab == true,如何设置显示的标签字符的宽度?
我想使用它作为插件的一个快速而肮脏的脚本输入框。它真的不需要花哨,但如果选项卡不显示为8个字符宽度将是很好的…
从接受的答案:
// set tab stops to a width of 4
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();
var characterWidth = (int)graphics.MeasureString("M",textBox.Font).Width;
SendMessage(textBox.Handle,EM_SETTABSTOPS,1,new int[] { tabWidth * characterWidth });
}
这可以在窗体的构造函数中调用,但要小心:确保首先运行InitializeComponents。
我认为发送EM_SETTABSTOPS消息到TextBox将工作。
> Link at MSDN
> Here is another link