views:

309

answers:

3

I'd like to be able to catch the DoubleClick or MouseDoubleClick events from a standard winforms radio button, but they seem to be hidden and not working. At the moment I have code like this:

public class RadioButtonWithDoubleClick : RadioButton
{
 public RadioButtonWithDoubleClick()
  : base()
 {
  this.SetStyle( ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true );
 }

 [EditorBrowsable( EditorBrowsableState.Always ), Browsable( true )]
 public new event MouseEventHandler MouseDoubleClick;
 protected override void OnMouseDoubleClick( MouseEventArgs e )
 {
  MouseEventHandler temp = MouseDoubleClick;
  if( temp != null ) {
   temp( this, e );
  }
 }
}

Is there a simpler and cleaner way to do it?

Edit: For background, I agree with Raymond Chen's post here that the ability to double click on a radio button (if those are the only controls on the dialog) makes the dialog just a tiny bit easier to use for people who know about it.

In Vista using Task Dialogs (see this Microsoft guideline page or this MSDN page specifically about the Task Dialog API) would be the obvious solution, but we don't have the luxury of that.

+2  A: 

You could do something like this:

myRadioButton.MouseClick += new MouseEventHandler(myRadioButton_MouseClick);

void myRadioButton_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Clicks == 2)
    {
         // Do something
    }
}

You may or may not also want to check that e.Button == MouseButtons.Left

Nate
I'm accepting this one, because although I think my own solution is simpler for my case, where I'm going to be using the same control many times, if someone just wanted one or two then yours is much simpler.
Ant
A: 

Sorry, don't have the reputation to comment on this. What action are you trying to have the double-click perform for the user? I think using a double-click may be confusing because it is different from the general mental model that a user has of a radio-button (IE single click, select one option from a set)

OG
Good point. I'll update the question
Ant
Ah, I see. You are using the double-click as a shortcut for select+next. That makes sense. I think the code you presented is the cleanest way to go about this. Looks like this is functionality that MS tried to intentionally bury for the radiobutton.
OG
That's what I figured :( Thanks for the feedback.
Ant
A: 

Based on your original suggestion I made a solution without the need to subclass the radiobuton using reflection:

MethodInfo m = typeof(RadioButton).GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
if (m != null)
{
    m.Invoke(radioButton1, new object[] { ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true });
}
radioButton1.MouseDoubleClick += radioButton1_MouseDoubleClick;

Now the double click event for the radiobutton is fired. BTW: The suggestion of Nate using e.Clicks doesn't work. In my tests e.Clicks was always 1 no matter how fast or often I clicked the radiobutton.

MSW