How to 'align' text in RichTextBox C#?

ⅰ亾dé卋堺 提交于 2019-11-29 06:07:08

You would have to change the font to a monospaced font, like Courier. This behavior you're showing is standard with most fonts, as not all characters are the same width.

You want to use the RichTextBox.SelectionAlignment property.

For instance if you want the whole textbox centered, then you would do:

richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;

If you want only part of the textbox with a certain alignment, then use the RichTextBox.Select() routine to select the text, then set the SelectionAlignment property.

Ahmed Sherif
richTextBox1.SelectAll();
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
richTextBox1.DeselectAll();

Unless it is very necessary for you to use a rich textbox, you can simply use a textbox and choose alignment as

textbox.TextAlign = HorizontalAlignment.Center;/*could be left, right or center*/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!