首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Windows Forms TextBox控件中设置选项卡宽度?

如何在Windows Forms TextBox控件中设置选项卡宽度?
EN

Stack Overflow用户
提问于 2009-08-19 07:59:46
回答 4查看 15.8K关注 0票数 24

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

我想使用它作为一个插件的快速和肮脏的脚本输入框。它真的不需要花哨,但如果制表符不显示为8个字符的宽度就好了……

EN

回答 4

Stack Overflow用户

发布于 2009-08-19 08:35:23

我知道您目前正在使用TextBox,但是如果您可以使用RichTextBox,那么您可以使用SelectedTabs属性来设置所需的选项卡宽度:

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

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

票数 11
EN

Stack Overflow用户

发布于 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 });
        }
    }
}
票数 6
EN

Stack Overflow用户

发布于 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);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1298406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档