Events in Windows Forms FontComboBox

6 Oct 20222 minutes to read

This section comprises the below events:

SelectedIndexChanged Event

SelectedIndexChanged event - This event is raised when the ComboBox.SelectedIndex property is changed.

The below code snippet, lets you set the selected font style, for a label, on selecting through a FontComboBox, using SelectedIndexChanged event.

private void fontComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    this.label1.Font = new Font(this.fontComboBox2.SelectedItem.ToString(), 11, FontStyle.Regular);
}
Private Sub fontComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Me.label1.Font = New Font(Me.fontComboBox2.SelectedItem.ToString(), 11, FontStyle.Regular)
End Sub

FontSelected Event

To add FontSelected event, derive the classes as shown below.

  1. Add an event in the derived class.

    // Adding event.
    public event System.EventHandler FontSelected;
    ' Adding event.
    Public Event FontSelected As System.EventHandler
  2. Override the OnSelectedIndexChanged method and fire the event there.

    protected override void OnSelectedIndexChanged(EventArgs e) 
    {
    // FontSelected event fires here.
    if(FontSelected!=null) FontSelected(this,e);
    	base.OnSelectedIndexChanged (e);
    }
    Protected Overrides Sub OnSelectedIndexChanged(ByVal e As EventArgs)
       
    ' FontSelected event fires here.
    RaiseEvent FontSelected(Me, e)
    MyBase.OnSelectedIndexChanged(e)
    End Sub