Click here to Skip to main content
Licence CPOL
First Posted 27 Jan 2012
Views 5,050
Bookmarked 0 times

Creating a Windows Forms RadioButton that supports the double click event

By Cyotek | 27 Jan 2012 | Technical Blog
C#
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 dia
   0.00 (0 votes)
 
A Technical Blog article. View original blog here.[^]

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).

  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)

About the Author

Cyotek

Software Developer (Senior)

United Kingdom United Kingdom

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120222.1 | Last Updated 27 Jan 2012
Article Copyright 2012 by Cyotek
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid