给定一个带有MultiLine = true
和AcceptsTab == true
的WinForms TextBox控件,如何设置显示的制表符的宽度?
我想使用它作为一个插件的快速和肮脏的脚本输入框。它真的不需要花哨,但如果制表符不显示为8个字符的宽度就好了……
发布于 2009-08-19 08:35:23
我知道您目前正在使用TextBox
,但是如果您可以使用RichTextBox
,那么您可以使用SelectedTabs属性来设置所需的选项卡宽度:
richTextBox.SelectionTabs = new int[] { 15, 30, 45, 60, 75};
注意,这些偏移量是像素,而不是字符。
发布于 2013-01-25 15:59:20
通过使用扩展方法,可以向TextBox控件类添加新方法。这是我的实现(包括一个额外的扩展方法,它为您提供插入插入符号的当前位置的坐标),来自我从上面的贡献者那里收集的信息:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Extensions
{
public static class TextBoxExtension
{
private const int EM_SETTABSTOPS = 0x00CB;
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, int[] lParam);
public static Point GetCaretPosition(this TextBox textBox)
{
Point point = new Point(0, 0);
if (textBox.Focused)
{
point.X = textBox.SelectionStart - textBox.GetFirstCharIndexOfCurrentLine() + 1;
point.Y = textBox.GetLineFromCharIndex(textBox.SelectionStart) + 1;
}
return point;
}
public static void SetTabStopWidth(this TextBox textbox, int width)
{
SendMessage(textbox.Handle, EM_SETTABSTOPS, 1, new int[] { width * 4 });
}
}
}
发布于 2014-10-30 19:15:37
对于任何想要不同标签宽度的人,我采取了以下方法:
using System.Runtime.InteropServices;
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, uint[] lParam);
private const int EM_SETTABSTOPS = 0x00CB;
private void InitialiseTabStops()
{
// Declare relative tab stops in character widths
var tabs = new uint[] { 2, 2, 4, 8, 2, 32 };
// Convert from character width to 1/4 character width
for (int position = 0; position < tabs.Length; position++)
tabs[position] *= 4;
// Convert from relative to absolute positions
for (int position = 1; position < tabs.Length; position++)
tabs[position] += tabs[position - 1];
SendMessage(textBox.Handle, EM_SETTABSTOPS, tabs.Length, tabs);
}
https://stackoverflow.com/questions/1298406
复制相似问题