ActionScript 2.0 Components Language Reference |
|
|
|
| Menu component > Menu.change | |||
Flash Player 6 (6.0.79.0).
Flash MX Professional 2004.
Usage 1:
var listenerObject:Object= new Object();listenerObject.change = function(eventObject:Object) {// Insert your code here.};menuInstance.addEventListener("change",listenerObject);
Usage 2:
on (change) {
// Insert your code here.
}
Event; broadcast to all registered listeners whenever a user causes a change in the menu.
Components use a dispatcher-listener event model. When a Menu component broadcasts a change event, the event is handled by a function (also called a handler) that is attached to a listener object (listenerObject) that you create. You call the addEventListener() method and pass it the name of the handler as a parameter.
When the event is triggered, it automatically passes an event object (eventObject) to the handler. Each event object has properties that contain information about the event. You can use these properties to write code that handles the event. The Menu.change event's event object has the following additional properties:
menuBar A reference to the MenuBar instance that is the parent of the target menu. When the target menu does not belong to a MenuBar instance, this value is undefined.menu A reference to the Menu instance where the target item is located.menuItem An XML node that is the menu item that was selected.groupName A string indicating the name of the radio button group to which the item belongs. If the item is not in a radio button group, this value is undefined.For more information, see EventDispatcher class.
The following example creates a menu, my_menu, and defines an event listener for it, menulistener, which listens for a change event. When a user causes a change event by clicking a menu item, the example displays its label attribute in the Output panel.
You first drag a Menu component to the library and then add the following code to Frame 1:
/**
Requires:
- Menu component in library
*/
import mx.controls.Menu;
// Create an XML object to act as a factory.
var my_xml:XML = new XML();
// The item created next does not appear in the menu.
// The createMenu() method call (below) expects to
// receive a root element whose children will become
// the items. This is just a simple way to create that
// root element and give it a convenient name.
var menuDP_obj:Object = my_xml.addMenuItem("Edit");
// Add the menu items.
menuDP_obj.addMenuItem({label:"1st Item"});
menuDP_obj.addMenuItem({label:"2nd Item"});
// Create the Menu object.
var my_menu:Menu = Menu.createMenu(this, menuDP_obj);
my_menu.show();
var menuListener:Object = new Object();
menuListener.change = function(evt_obj:Object) {
trace("Menu item chosen: " + evt_obj.menuItem.attributes.label);
};
my_menu.addEventListener("change", menuListener);
|
|
|
|