RichTextBox.SelectionAlignment RichTextBox.SelectionAlignment RichTextBox.SelectionAlignment RichTextBox.SelectionAlignment Property

定义

获取或设置应用到当前选定内容或插入点的对齐方式。Gets or sets the alignment to apply to the current selection or insertion point.

public:
 property System::Windows::Forms::HorizontalAlignment SelectionAlignment { System::Windows::Forms::HorizontalAlignment get(); void set(System::Windows::Forms::HorizontalAlignment value); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.HorizontalAlignment SelectionAlignment { get; set; }
member this.SelectionAlignment : System.Windows.Forms.HorizontalAlignment with get, set
Public Property SelectionAlignment As HorizontalAlignment

属性值

异常

指定的值不是 HorizontalAlignment 类中定义的值之一。The specified value is not one of the values defined in the HorizontalAlignment class.

示例

下面的代码示例演示如何对齐中的RichTextBox文本。The following code example demonstrates how to align text within the RichTextBox. 此示例要求RichTextBox已将名为richTextBox1的控件添加到窗体中。This example requires that a RichTextBox control, named richTextBox1, has been added to the form.

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 = gcnew System::Drawing::Font( "Lucinda Console",12 );

      // Set the text within the control.
      richTextBox1->SelectedText = "This text is centered using the SelectionAlignment property.\n";
   }
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";
}
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

注解

如果在控件中未选择任何段落,则设置此属性会将对齐设置应用到插入点所在的段落以及在具有对齐属性设置的段落之后创建的段落。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. 例如,如果RichTextBox控件中有两个段,并且插入点位于第二个段落中。For example, if there are two paragraphs in a RichTextBox control and the insertion point is located within the second paragraph. 如果将SelectionAlignment属性设置为HorizontalAlignment.Center,则插入点的段落将在控件中居中。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. 您可以使用此属性来对齐在中RichTextBox创建的文档中的段落。You can use this property to align the paragraphs in a document being created in the RichTextBox. 例如,如果希望文档中的所有段落都居中,则可以选择控件中的所有段落并将SelectionAlignment属性设置为。 HorizontalAlignment.CenterFor 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.

备注

SelectionAlignmentSelectionAlignment.Left所选文本包含多个具有混合对齐方式的段落时返回。SelectionAlignment returns SelectionAlignment.Left when the text selection contains multiple paragraphs with mixed alignment.

适用于

另请参阅