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 宽度?


基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01