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

前端 未结 6 499
独厮守ぢ
独厮守ぢ 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:25

    The example offered is incorrect.

    The EM_SETTABSTOPS message expects the tab sizes to be specified in dialog template units and not in pixels. After some digging around, it appears that a dialog template unit equals to 1/4th the average width of the window's character. So you'll need to specify 8 for 2 characters long tabs, 16 for four charachters, and so on.

    So the code can be simplified as:

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

提交回复
热议问题