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.