How to 'align' text in RichTextBox C#?

泪湿孤枕 提交于 2019-11-27 23:36:15

问题


How do I align the text in a RichTextBox?

Basically, the RTB contains:

"--testing"

"--TESTING"

"TESTING--"

"testing--"

Which all have the same number of characters, but have different alignments. How can I align them properly? Im fairly new to C# and confused since it aligned properly in Java's TextArea.

Thank you!


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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*/


来源:https://stackoverflow.com/questions/6243350/how-to-align-text-in-richtextbox-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!