Flex/AIR: DynamicEvent

View Comments

Applies to: Adobe Flex & AIR

A DynamicEvent is a type of custom event in Flex that is easier and quicker to set up than the full-blown approach. By applying the latter method, you normally go through the following steps:

  1. Define a subclass from flash.events.Event
  2. Make the event available to other ActionScript (through addEventListenter()) components or MXML components (using the [Event] metatag)
  3. Dispatch the event, using dispatchEvent()

Full documentation on custom events can be found in Adobe’s LiveDocs, here.

Generally one uses the above approach when you need to pass arguments to the code that’s targeted by the event. When you don’t require this, however, you can opt for the DynamicEvent. It’s not necessary to go through the whole rigamarole of subclassing; all you need is a name for the event, like so:

var yourEvent:DynamicEvent = new DynamicEvent("yourEventName", true);
dispatchEvent(yourEvent);

The second argument set to true means the event will bubble.

  • Twitter
  • Slashdot
  • Instapaper
  • Digg
  • Facebook
  • Mixx
  • Delicious
  • Reddit
  • FriendFeed
  • Google Buzz
  • StumbleUpon
  • Evernote
  • Share/Bookmark

AS3: How to set focus to an empty textfield

View Comments

Applicable to: Adobe ActionScript 3, Adobe Flex, Adobe AIR

This simple operation isn’t very obvious in ActionScript 3. The TextInput element has a boolean property called focusEnabled, but this doesn’t seem to do anything (it’s probably meant for other purposes, didn’t take the time to check).

The way achieve this is two-fold:

stage.focus = yourTextField;
yourTextField.setSelection(0,0);

If your textfield contains text and you wish to place the caret to the end, you would probably go about it like so:

stage.focus = yourTextField;
yourTextField.setSelection(0, yourTextField.length);

Makes sense now you know about it, huh?

  • Twitter
  • Slashdot
  • Instapaper
  • Digg
  • Facebook
  • Mixx
  • Delicious
  • Reddit
  • FriendFeed
  • Google Buzz
  • StumbleUpon
  • Evernote
  • Share/Bookmark