リッチテキストボックスで選択したテキストを太字体にできるようにするには、〈太字〉ボタンの RibbonToggleButton.Click イベントハンドラを作成します。 以下のコードをプロジェクトに追加します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
' 名前空間の Imports ディレクティブを入力
Imports C1.Win.C1Ribbon
' 〈太字〉ボタンの Click イベントを処理
Private Sub BoldBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BoldBtn.Click
' 太字〉ボタンにスタイルを割り当て
ToggleSelectionFontStyle(FontStyle.Bold)
End Sub
' RichTextBox にフォントスタイルを適用
Sub ToggleSelectionFontStyle(ByVal fontStyle As FontStyle)
If Me.RichTextBox1.SelectionFont Is Nothing Then
MessageBox.Show("Cannot change font style while selected text has more than one font.")
Else
Me.RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, Me.RichTextBox1.SelectionFont.Style Xor fontStyle)
End If
Me.RichTextBox1.Focus
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
// 名前空間の Imports ディレクティブを入力
using C1.Win.C1Ribbon;
// 〈太字〉ボタンの Click イベントを処理
private void BoldBtn_Click(object sender, EventArgs e)
{
// 〈太字〉ボタンにスタイルを割り当て
ToggleSelectionFontStyle(FontStyle.Bold);
}
// RichTextBox にフォントスタイルを適用
void ToggleSelectionFontStyle(FontStyle fontStyle)
{
if (this.richTextBox1.SelectionFont == null)
{
MessageBox.Show("Cannot change font style while selected text has more than one font.");
}
else
{
this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont,
this.richTextBox1.SelectionFont.Style ^ fontStyle);
}
this.richTextBox1.Focus();
}
|
|
この例では、RibbonToggleButton.Name プロパティが BoldBtn に設定されている点に注意してください。