如何在Windows窗体TextBox控件中设置TAB宽度?

时间:2009-08-19 07:59:46

标签: .net winforms textbox

给定带有MultiLine = trueAcceptsTab == true的WinForms TextBox控件,如何设置显示的制表符宽度?

我想将它用作插件的快速和脏脚本输入框。它根本不需要花哨,但如果标签没有显示为8个字符宽,那就太好了......

6 个答案:

答案 0 :(得分:13)

我认为将EM_SETTABSTOPS消息发送到TextBox会起作用。

// 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

答案 1 :(得分:8)

我知道您当前正在使用TextBox,但如果您可以使用RichTextBox代替,那么您可以使用SelectedTabs属性设置所需的标签宽度:

richTextBox.SelectionTabs = new int[] { 15, 30, 45, 60, 75};

请注意,这些偏移是像素,而不是字符。

答案 2 :(得分:6)

提供的示例不正确。

EM_SETTABSTOPS消息需要在dialog template units中指定标​​签大小,而不是以像素为单位。经过一番挖掘后,对话框模板单元似乎等于1/4th the average width of the window's character。因此,您需要为2个字符的长标签指定8,为4个字符指定16,依此类推。

因此代码可以简化为:

public static void SetTabWidth(TextBox textbox, int tabWidth)
{
    SendMessage(textbox.Handle, EM_SETTABSTOPS, 1, 
            new int[] { tabWidth * 4 });
}

答案 3 :(得分:6)

使用扩展方法,可以向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 });
        }
    }
}

答案 4 :(得分:2)

答案 5 :(得分:2)

对于任何想要不同标签宽度的人,我采用了这种方法:

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);
}