SelectionAlignment Property

RichTextBox.SelectionAlignment Property

Gets or sets the alignment to apply to the current selection or insertion point.

[Visual Basic]
Public Property SelectionAlignment As HorizontalAlignment
[C#]
public HorizontalAlignment SelectionAlignment {get; set;}
[C++]
public: __property HorizontalAlignment get_SelectionAlignment();
public: __property void set_SelectionAlignment(HorizontalAlignment);
[JScript]
public function get SelectionAlignment() : HorizontalAlignment;
public function set SelectionAlignment(HorizontalAlignment);

Property Value

One of the HorizontalAlignment values.

Exceptions

Exception Type Condition
InvalidEnumArgumentException The specified value is not one of the values defined in the HorizontalAlignment class.

Remarks

If no paragraph is selected in the control, setting this property applies the alignment setting to the paragraph in which the insertion point appears as well as to paragraphs created after the paragraph that has the alignment property setting. For example, if there are two paragraphs in a RichTextBox control and the insertion point is located within the second paragraph. If you set the SelectionAlignment property to HorizontalAlignment.Center, the paragraph at the insertion point will be centered within the control. If a third paragraph is created after the second paragraph, it also is aligned to the center of the control.

If a selection is made within the control when the property is set, all paragraphs selected are aligned based on this property setting. You can use this property to align the paragraphs in a document being created in the RichTextBox. For example, if you want all paragraphs in a document to be centered, you can select all the paragraphs in the control and set the SelectionAlignment property to HorizontalAlignment.Center.

Note    SelectionAlignment returns SelectionAlignment.Left when the text selection contains multiple paragraphs with mixed alignment.

Example

[Visual Basic, C#, C++] The following example demonstrates how to align text within the RichTextBox. This example assumes that a RichTextBox control, named richTextBox1, has been added to the form.

[Visual Basic] 
Private Sub WriteCenteredTextToRichTextBox()
   ' Clear all text from the RichTextBox;
   richTextBox1.Clear()
   ' Set the foreground color of the text.
   richTextBox1.SelectionColor = Color.Red
   ' Set the alignment of the text that follows.
   richTextBox1.SelectionAlignment = HorizontalAlignment.Center
   ' Set the font for the text.
   richTextBox1.SelectionFont = new Font("Lucinda Console", 12)
   ' Set the text within the control.
   richTextBox1.SelectedText = "This text is centered using the SelectionAlignment property."

End Sub

[C#] 
private void WriteCenteredTextToRichTextBox()
{
   // Clear all text from the RichTextBox;
   richTextBox1.Clear();
   // Set the foreground color of the text.
   richTextBox1.ForeColor = Color.Red;
   // Set the alignment of the text that follows.
   richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
   // Set the font for the text.
   richTextBox1.SelectionFont = new Font("Lucinda Console", 12);
   // Set the text within the control.
   richTextBox1.SelectedText = "This text is centered using the SelectionAlignment property.\n";
}

[C++] 
private:
    void WriteCenteredTextToRichTextBox()
    {
        // Clear all text from the RichTextBox;
        richTextBox1->Clear();
        // Set the foreground color of the text.
        richTextBox1->ForeColor = Color::Red;
        // Set the alignment of the text that follows.
        richTextBox1->SelectionAlignment = HorizontalAlignment::Center;
        // Set the font for the text.
        richTextBox1->SelectionFont = new System::Drawing::Font(S"Lucinda Console", 12);
        // Set the text within the control.
        richTextBox1->SelectedText = S"This text is centered using the SelectionAlignment property.\n";
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

RichTextBox Class | RichTextBox Members | System.Windows.Forms Namespace | HorizontalAlignment

Show:
© 2016 Microsoft