Wednesday, 14 February, 2007

Disabling a Flash SimpleButton

The enabled property of the flash.display.SimpleButton class, according to the Flex Language Reference, is supposed to enable and disable a button.  The documentation reads:

A Boolean value that specifies whether a button is enabled. When a button is disabled (the enabled property is set to false), the button is visible but cannot be clicked. The default value is true. This property is useful if you want to disable part of your navigation; for example, you might want to disable a button in the currently displayed page so that it can't be clicked and the page cannot be reloaded.

Unfortunately, it doesn't seem to work.

If I set enabled to false, the button no longer responds to the mouse moving over it.  That is, the button states don't change when the user moves over or clicks on the button.  But a click on the button does result in a CLICK event being sent to the event handler.  So although enabled prevents the display state from changing, it does not prevent the button from raising events.  In other words, enabled is useless.

However, you can disable a button by setting the hitTestState to null.  The Flash runtime tests the mouse position against the hitTestState object to determine if the mouse is over the button.  hitTestState is usually set to the same value as upState, but if hitTestState is null then the button has no presence as far as the hit testing code is concerned, making it impossible to roll over or click on the button.

So I wrote the following little function to enable or disable a SimpleButton object:

private function enableButton(button:SimpleButton, bEnable : Boolean) : void
    if (bEnable)
	button.hitTestState = button.upState;
	button.hitTestState = null;

It seems like the enabled property should work as advertised. I wonder if I have an older version of the Flash 9 runtime, and that bug is fixed in an update. I guess I'll have to go hunting.