現在選択されている内容またはカーソル位置に適用される配置を取得または設定します。
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);
プロパティ値
HorizontalAlignment 値の 1 つ。
例外
例外の種類 | 条件 |
---|---|
InvalidEnumArgumentException | 指定した値が HorizontalAlignment クラスに定義されている値ではありません。 |
解説
コントロール内で選択されている段落がない場合、このプロパティに設定した配置は、その設定が適用されている段落の後に作成された段落だけでなく、カーソルが挿入されている段落にも適用されます。たとえば、 RichTextBox コントロールに 2 つの段落があり、カーソルが 2 番目の段落内にあるとします。 SelectionAlignment プロパティを HorizontalAlignment.Center に設定すると、カーソルが挿入されている段落がコントロールの中央に揃えて配置されます。2 番目の段落の後に 3 番目の段落を作成すると、3 番目の段落もコントロールの中央に揃えて配置されます。
プロパティを設定するときにコントロール内で段落が選択されている場合は、選択されているすべての段落が、このプロパティの設定に基づいて配置されます。このプロパティを使用すると、 RichTextBox で作成中のドキュメントの段落を揃えることができます。たとえば、ドキュメント内のすべての段落を中央に揃えて配置する場合は、コントロール内のすべての段落を選択し、 SelectionAlignment プロパティを HorizontalAlignment.Center に設定します。
メモ 選択したテキストに、さまざまな配置の混在した段落が複数含まれている場合、 SelectionAlignment は SelectionAlignment.Left を返します。
使用例
[Visual Basic, C#, C++] RichTextBox 内でテキストを配置する方法を次の例に示します。この例は、 richTextBox1
という名前の RichTextBox コントロールがフォームに追加されていることを前提にしています。
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間 | HorizontalAlignment