Using C#, how do I set tab positions in a multilin

2019-06-21 18:17发布

问题:

Is there a graceful way to set custom tab sizes/positions in a multiline textbox in C#?

回答1:

You need to send the EM_SETTABSTOPS message, like this:

static class NativeMethods {
    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref int lParam);
}
static void SetTabs(TextBox box) {
    //EM_SETTABSTOPS - http://msdn.microsoft.com/en-us/library/bb761663%28VS.85%29.aspx
    int lParam = 16;  //Set tab size to 4 spaces
    NativeMethods.SendMessage(box.Handle, 0x00CB, new IntPtr(1), ref lParam);
    box.Invalidate();
}


回答2:

Apart from by vb 2013 the friendly people at microsoft have decided you no longer need the windows handle and you can no longer get at it.