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 段落,且插入點位於第二個段落內。 如果您將 SelectionAlignment 屬性設定為 HorizontalAlignment.Center ,插入點上的段落將會置中于 控制項內。 如果在第二個段落之後建立第三個段落,它也會對齊控制項的中心。

如果在設定 屬性時,在控制項內進行選取,則選取的所有段落都會根據這個屬性設定來對齊。 您可以使用這個屬性來對齊 檔中 RichTextBox 所建立的段落。 例如,如果您想要讓檔中的所有段落置中,您可以選取控制項中的所有段落,並將 屬性設定 SelectionAlignmentHorizontalAlignment.Center

注意

SelectionAlignmentSelectionAlignment.Left當文字選取範圍包含具有混合對齊方式的多個段落時,會傳回 。

適用於

另請參閱