RichTextBox.SelectionAlignment 属性

定义

获取或设置应用到当前选定内容或插入点的对齐方式。

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; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionAlignment : System.Windows.Forms.HorizontalAlignment with get, set
Public Property SelectionAlignment As HorizontalAlignment

属性值

HorizontalAlignment 值之一。

属性

例外

指定的值不是 HorizontalAlignment 类中定义的值之一。

示例

下面的代码示例演示如何在 内 RichTextBox对齐文本。 此示例要求 RichTextBox 已将名为 richTextBox1的控件添加到窗体中。

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

注解

如果在 控件中未选择任何段落,则设置此属性会将对齐设置应用于显示插入点的段落以及具有对齐属性设置的段落之后创建的段落。 例如,如果一个控件中有两个 RichTextBox 段落,并且插入点位于第二个段落内。 如果将 属性设置为 SelectionAlignmentHorizontalAlignment.Center,则插入点处的段落将在 控件中居中。 如果在第二个段落之后创建第三个段落,它也与控件的中心对齐。

如果在设置 属性时在 控件中进行选择,则根据此属性设置对齐所有所选段落。 可以使用此属性来对齐 在 中创建的文档中的 RichTextBox段落。 例如,如果希望文档中的所有段落都居中,则可以选择控件中的所有段落,并将 属性设置为 SelectionAlignmentHorizontalAlignment.Center

注意

SelectionAlignmentSelectionAlignment.Left当文本选择包含具有混合对齐方式的多个段落时返回 。

适用于

另请参阅