ChP
ChP

Reputation: 485

Why does a button handle a click differently to non-buttons

This may sound like a really silly question but I've noticed that the Click behaviour of my custom buttons differ when I inherit my class from a Button or UserControl.

I'm developing some controls with a customized look, among others, a button. The default user control class declaration is like this:

public partial class cButton : UserControl

After I added all of the GUI stuff, I added it to my form and tested the click-behaviour.

When I click the button in rapid succession, it only registers ever other click, not even every other click. I thought there is something wrong with the test code, but when I copied the exact code to a normal Winforms button, it registered every click no matter how fast.

Edit: the user control registers every click if I don't click to fast i.e. I wait a few seconds between every click.

I changed my custom control's decleration to inherit from the button class and made absolutely no other changes to any code:

public partial class cButton : Button

When I did my click-test the custom button behaved well, like a winforms button, not missing a click.

Just to test things, I added a list box to my form and added the same test code to its click event and it acted like a non-button, only registering a click every now and then.

I thought a click is supposed to be handled consistently, but apparently it's not that simple.

The question I have arising from this:

What does a button do differently and what could I do to ensure proper click-behaviour when it is not possible to inherit from a Button?

Upvotes: 3

Views: 130

Answers (2)

Lemonseed
Lemonseed

Reputation: 1732

Your custom UserControl is differentiating between single clicks and double clicks.

To make it operate like a button, you need to set the StandardDoubleClick control style so that when the user clicks twice in rapid succession, the control registers two single clicks and raises two click events, and not a double click event.

Within the constructor add the following statement:

this.SetStyle(ControlStyles.StandardDoubleClick, false);

Upvotes: 1

Charles May
Charles May

Reputation: 1763

I think the issue you're experiencing is that if you click the button too fast it registers as a double-click instead of a click. You can check this by writing to your output on double-click so that if your codes doesn't fire, check to see if double-click event did.

Upvotes: 1

Related Questions