ComponentOne Ribbon for WinForms
Handling the RibbonToggleButton.Click Event
C1Ribbon (Classic) Task-Based Help > Handling Ribbon Events > Handling the RibbonToggleButton.Click Event
Note: This topic assumes that you have added a RibbonToggleButton to the Ribbon and a RichTextBox control to the Ribbon Form. For steps on how to add a Ribbon control to the Ribbon, see Adding Items to the Ribbon Group.

To make the selected text bold for a rich text box, create a RibbonToggleButton.Click event handler for the Bold button. Add the following code to your project:

To write code in Visual Basic

Visual Basic
Copy Code
' type the Imports directive for the namespace
Imports C1.Win.C1Ribbon
 
' handles the Click event for the Bold button
Private Sub BoldBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BoldBtn.Click
    ' assign style for Bold button
    ToggleSelectionFontStyle(FontStyle.Bold)
End Sub
 
' apply font style to the 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

To write code in C#

C#
Copy Code
// type the using directive for the namespace
using C1.Win.C1Ribbon;
 
// handles the Click event for the Bold button
private void BoldBtn_Click(object sender, EventArgs e)
{
    // assign style for Bold button
    ToggleSelectionFontStyle(FontStyle.Bold);
}
 
// apply font style to the 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();
}

Note that the RibbonToggleButton.Name property has been set to BoldBtn for this example.