How to set the TAB width in a Windows Forms TextBox control?

前端 未结 6 497
独厮守ぢ
独厮守ぢ 2020-12-17 08:57

Given a WinForms TextBox control with MultiLine = true and AcceptsTab == true, how can I set the width of the tab character displayed?

I wa

6条回答
  •  离开以前
    2020-12-17 09:33

    For anyone who wants different tab widths, I took this approach:

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

提交回复
热议问题