Click here to Skip to main content
15,893,564 members
Articles / Programming Languages / C#

Creating a Windows Forms RadioButton that Supports the Double Click Event

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
27 Jan 2012CPOL1 min read 11.2K  
How to create a Windows Forms RadioButton that supports the double click event

Another of the peculiarities of Windows Forms is that the RadioButton control doesn't support double clicking. Granted, it is not often you require the functionality but it's a little odd it's not supported.

As an example, one of our earlier products which never made it to production uses a popup dialog to select a zoom level for a richtext box. Common zoom levels are provided via a list of radio buttons. Rather than the user having to first click a zoom level and then click the OK button, we wanted the user to be able to simply double click an option to have it selected and the dialog close.

However, once again with a simple bit of overriding magic, we can enable this functionality.

Create a new component and paste in the code below (using and namespace statements omitted for clarity).

C#
public partial class RadioButton : System.Windows.Forms.RadioButton
{
  public RadioButton()
  {
    InitializeComponent();

    this.SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true);
  }

  [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
  public new event MouseEventHandler MouseDoubleClick;

  protected override void OnMouseDoubleClick(MouseEventArgs e)
  {
    base.OnMouseDoubleClick(e);

    // raise the event
    if (this.MouseDoubleClick != null)
      this.MouseDoubleClick(this, e);
  }
}

This new component inherits from the standard RadioButton control and unlocks the functionality we need.

The first thing we do in the constructor is to modify the components ControlStyles to enable the StandardDoubleClick style. At the same time, we also set the StandardClick style as the MSDN documentation states that StandardDoubleClick will be ignored if StandardClick is not set.

As you can't override an event, we declare a new version of the MouseDoubleClick event using the new keyword. To this new definition, we add the EditorBrowsable and Browsable attributes so that the event appears in the IDE property inspectors and intellisense.

Finally, we override the OnMouseDoubleClick method and invoke the MouseDoubleClick event whenever this method is called.

And there we have it. Three short steps and we now have a radio button that you can double click.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --