Reputation: 3965
how can I add a double click event to a control that doesn't have a double click event =P
like a combo box!!!
Upvotes: 0
Views: 1950
Reputation: 4152
you don't double click it, you make it look like you double-clicked it, if you are evil enough.
private void box_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
TimeSpan Current = DateTime.Now - LastClick;
TimeSpan DblClickSpan =
TimeSpan.FromMilliseconds(SystemInformation.DoubleClickTime);
if (Current.TotalMilliseconds <= DblClickSpan.TotalMilliseconds)
{
// Code to handle double click goes here
}
LastClick = DateTime.Now;
}
}
Upvotes: 3
Reputation: 64218
Actually a System.Windows.Forms.ComboBox has a DoubleClick event, it's just hidden from your editor:
// Summary:
// This event is not relevant for this class.
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public event EventHandler DoubleClick;
I'm guessing they chose to hide the event because it won't ever be called :)
Upvotes: 4
Reputation: 26271
I was unable to find a way (given that the text box eats the event); I also saw a note that double click is not relevant for this control. I also did not find a way to capture the event from the inner text control
You may want to rethink why you want to change the behavior from the default (namely, selecting the current text). If you change expected behavior too much, your application becomes that much harder to use.
Upvotes: 0
Reputation: 26190
The short answer is you don't.
The long answer is you subscribe to the Click event and see if another click event was called in the last XXX milliseconds, as in this post.
Upvotes: 3
Reputation: 16342
The TextBox control inside the combobox steals/consumes the DoubleClick event, so you have to use the Click event on the ComboBox to listen for both clicks?
Upvotes: 0
Reputation: 18615
According to my copy of Reflector, System.Windows.Forms.ComboBox, from assembly System.Windows.Forms, Version 2.0.0.0, DOES have a DoubleClick event.
Upvotes: 0
Reputation: 106912
<sarcasm>
Use more exclamation points - that usually gets the job done!</sarcasm>
But, seriously, you can't. Try checking for the simple "Click" event and then see if the time between two successive clicks is small enough. I'm not sure where you can find the system double-click timing though. Try googling for that.
However, I would seriously think twice about adding such non-standard behavior to a standard control. Users normally don't expect this so they will quite likely be unhappy about this. Remember - the best UI is the one that offers the user least surprises. Better think of another way to do what you are trying to do.
Upvotes: 6