Ribbon for WinForms
手順 3:トグルボタンにイベントハンドラを追加する
クイックスタート > 手順 3:トグルボタンにイベントハンドラを追加する

この手順では、リボンタブやグループ名として表示されるテキストを変更します。さらに、リボンのフォントグループにある[太字]、[斜体]、[下線]の各リボンボタンを有効にするためのイベントハンドラを追加します。

トグルボタンにイベントハンドラを追加する前に、タブの Text プロパティを「ホーム」に設定し、グループの Text プロパティを「フォント」に設定します。これを行うには、以下の手順を実行します

  1. リボン上のタブのテキスト部分をダブルクリックし、強調表示にします。タブのテキストを編集できるようになります。
  2. 「ホーム」と入力します。
  3. [Enter]を押すか、編集ボックスの外でクリックし、変更を確定します。タブ名が「ホーム」と表示されます。
  4. リボン上のグループのテキスト部分をダブルクリックし、強調表示にします。グループのテキストを編集できるようになります。
  5. 「フォント」と入力します。
  6. [Enter]を押すか、編集ボックスの外でクリックし、変更を確定します。グループ名が「フォント」と表示されます。

リボングループは、次のように表示されます。

リボンの「フォント」グループの3つのトグルボタン([太字]、[斜体]、[下線])を機能させるには、コードエディタで次のコードを入力します。

Visual Basic コードの書き方

Visual Basic
コードのコピー
' 名前空間の Imports ディレクティブを入力します
Imports C1.Win.C1Ribbon
 
' [太字]の Click イベントを処理します 
Private Sub RibbonToggleButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RibbonToggleButton1.Click
    ' [太字]ボタンのスタイルを割り当てます
    ToggleSelectionFontStyle(FontStyle.Bold)
End Sub
 
' [斜体]ボタンの Click イベントを処理します
Private Sub RibbonToggleButton2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RibbonToggleButton2.Click
    ' [斜体]ボタンのスタイルを割り当てます
    ToggleSelectionFontStyle(FontStyle.Italic)
End Sub
 
' [下線]ボタンの Click イベントを処理します
Private Sub RibbonToggleButton3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RibbonToggleButton3.Click
    ' [下線]ボタンのスタイルを割り当てます
    ToggleSelectionFontStyle(FontStyle.Underline)
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#
コードのコピー
// 名前空間の using ディレクティブを入力します
using C1.Win.C1Ribbon;
 
// [太字]ボタンの Click イベントを処理します
private void ribbonToggleButton1_Click(object sender, EventArgs e)
{
    // [太字]ボタンのスタイルを割り当てます
    ToggleSelectionFontStyle(FontStyle.Bold);
}
 
// [斜体]ボタンの Click イベントを処理します
private void ribbonToggleButton2_Click(object sender, EventArgs e)
{
    // [斜体]ボタンのスタイルを割り当てます
    ToggleSelectionFontStyle(FontStyle.Italic);
}
 
// [下線]ボタンの Click イベントを処理します
private void ribbonToggleButton3_Click(object sender, EventArgs e)
{
    // [下線]ボタンのスタイルを割り当てます
    ToggleSelectionFontStyle(FontStyle.Underline);
}
 
// 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();
}

これで、RibbonGroup1 の3つのトグルボタンにイベントハンドラを追加できました。次の手順では、左ペインにプログレスバーを追加し、右ペインにトラックバーを追加して、ステータスバーを設定します。