.net – 如何在Windows Forms TextBox控件中设置TAB宽度?

前端之家收集整理的这篇文章主要介绍了.net – 如何在Windows Forms TextBox控件中设置TAB宽度?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给定一个WinForms TextBox控件,MultiLine = true和AcceptsTab == true,如何设置显示标签字符的宽度?

我想使用它作为插件的一个快速而肮脏的脚本输入框。它真的不需要花哨,但如果选项卡不显示为8个字符宽度将是很好的…

从接受的答案:

  1. // set tab stops to a width of 4
  2. private const int EM_SETTABSTOPS = 0x00CB;
  3.  
  4. [DllImport("User32.dll",CharSet = CharSet.Auto)]
  5. public static extern IntPtr SendMessage(IntPtr h,int msg,int wParam,int[] lParam);
  6.  
  7. public static void SetTabWidth(TextBox textBox,int tabWidth)
  8. {
  9. Graphics graphics = textBox.CreateGraphics();
  10. var characterWidth = (int)graphics.MeasureString("M",textBox.Font).Width;
  11. SendMessage(textBox.Handle,EM_SETTABSTOPS,1,new int[] { tabWidth * characterWidth });
  12. }

这可以在窗体的构造函数调用,但要小心:确保首先运行InitializeComponents。

我认为发送EM_SETTABSTOPS消息到TextBox将工作。

> Link at MSDN
> Here is another link

猜你在找的Windows相关文章