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.
All content Copyright (c) by Cyotek Ltd or its respective writers. Permission to reproduce news and web log entries and other RSS feed content in unmodified form without notice is granted provided they are not used to endorse or promote any products or opinions (other than what was expressed by the author) and without taking them out of context. Written permission from the copyright owner must be obtained for everything else.
Original URL of this content is https://www.cyotek.com/blog/creating-a-windows-forms-radiobutton-that-supports-the-double-click-event?source=rss.