How to set the TAB width in a Windows Forms TextBox control?(如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?)
问题描述
给定一个带有 MultiLine = true
和 AcceptsTab == true
的 WinForms TextBox 控件,如何设置显示的制表符的宽度?
Given a WinForms TextBox control with MultiLine = true
and AcceptsTab == true
, how can I set the width of the tab character displayed?
我想将其用作插件的快速而肮脏的脚本输入框.真的完全不需要花哨,但如果标签不显示为 8 个字符宽就好了……
I want to use this as a quick and dirty script input box for a plugin. It really doesn't need to be fancy at all, but it would be nice if tabs were not displayed as 8 characters wide...
推荐答案
我认为将 EM_SETTABSTOPS
消息发送到 TextBox 会起作用.
I think sending the EM_SETTABSTOPS
message to the TextBox will work.
// 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 }
);
}
这可以在 Form
的构造函数中调用,但要注意:确保首先运行 InitializeComponents
.
This can be called in the constructor of your Form
, but beware: Make sure InitializeComponents
is run first.
- 链接在 MSDN李>
- 这是另一个链接
这篇关于如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?


- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01