Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'align' text in RichTextBox C#?

How do I align the text in a RichTextBox?

RTB

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!

like image 229
user488792 Avatar asked Jun 05 '11 13:06

user488792


People also ask

How do I align text in Rich text?

You want to use the RichTextBox. SelectionAlignment property. richTextBox1. SelectAll(); richTextBox1.

How do you change the alignment of a TextBox?

Right-click the text box for which you want to set vertical alignment. On the shortcut menu, click Format Text Box. In the Format Text Box dialog box, click the Text Box tab. In the Vertical alignment box, select Top, Middle, or Bottom.

What is difference between RichTextBox and TextBox?

The RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox allows you to set the Font, as well as other formatting properties, for selections within the text displayed in the control.


1 Answers

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.

like image 186
Jason Moore Avatar answered Sep 18 '22 23:09

Jason Moore