Modifying default tab size in RichTextBox

后端 未结 5 751
北海茫月
北海茫月 2020-12-11 01:07

Is there any way to change the default tab size in a .NET RichTextBox? It currently seems to be set to the equivalent of 8 spaces which is kinda large for my taste.

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 01:25

    Winforms doesn't have a property to set the default tab size of a RichTexBox with a single number, but if you're prepared to dig into the Rtf of the rich text box, and modify that, there's a setting you can use called: "\deftab". The number afterwards indicates the number of twips (1 point = 1/72 inch = 20 twips). The resulting Rtf with the standard tab size of 720 twips could look something like:

    {\rtf1\ansi\ansicpg1252\deff0\deflang2057\deftab720{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\f0\fs41
    1\tab 2\tab 3\tab 4\tab 5\par
    }
    

    If you need to convert twips into pixels, use this code inspired from Convert Pixels to Points:

    int tabSize=720;
    Graphics g = this.CreateGraphics();
    int pixels = (int)Math.Round(((double)tabSize) / 1440.0 * g.DpiX);
    g.Dispose();
    

提交回复
热议问题