2 Replies Latest reply on Jul 3, 2006 7:30 PM by Newsgroup_User

    MenuBar Menu Items Events?

    Level 7
      Hi

      I have just started playing with Flex 2 Builder and I have built a simple
      app to get my feet wet. I have a menubar at the top of my app with three
      menus each with three choices

      MENU1
      SUB1A
      SUB1B
      SUB1C
      MENU2
      SUB2A
      SUB2B
      SUB2C
      MENU3
      SUB3A
      SUB3B
      SUB3C

      My problem is that there does not seem to be a way to access the sub menu
      items and assign events to each. When I put an onClick event in the
      <mx:MenuBar> it fires regardless of which submenu item I choose.

      Is there a way, or what is the best way, to create individual events for
      each submenu? Additionally, can I add additional elements to my <menuitem>
      tags such as <menuitem label="SUB3A" someValue="XYZ"> and then access
      "someValue" from whatever event I get to fire when the sub menu is clicked?

      Thanks

      Rich


        • 1. Re: MenuBar Menu Items Events?
          ntsiii Level 3
          When the event fires, the event object passed into the handler can be used to access the item that created the menu option. You can use information in the item to decide what action to take. Here is a simple example:

          Tracy

          <?xml version="1.0"?>
          <!-- Simple example to demonstrate the MenuBar control. -->
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="initCollections();" >

          <mx:Script>
          <![CDATA[

          import mx.events.MenuEvent;
          import mx.controls.Alert;
          import mx.collections.*;

          [Bindable]
          public var menuBarCollection:XMLListCollection;

          private var menubarXML:XML =
          <root>
          <menuitem label="Menu1">
          <menuitem label="MenuItem 1-A" data="1A"/>
          <menuitem label="MenuItem 1-B" data="1B"/>
          </menuitem>
          <menuitem label="Menu2">
          <menuitem label="MenuItem 2-A" type="check" data="2A"/>
          <menuitem type="separator" />
          <menuitem label="MenuItem 2-B" >
          <menuitem label="SubMenuItem 3-A" type="radio"
          groupName="one" data="3A"/>
          <menuitem label="SubMenuItem 3-B" type="radio"
          groupName="one" data="3B"/>
          </menuitem>
          </menuitem>
          </root>;

          // Event handler for the MenuBar control's change event.
          private function menuHandler(event:MenuEvent):void
          {
          Alert.show("Label: " + event.item.@label + "Data: " +
          event.item.@data, "Clicked menu item");
          }

          // Event handler to initialize the MenuBar control.
          private function initCollections():void
          {
          menuBarCollection = new XMLListCollection(menubarXML.children());
          }
          ]]>
          </mx:Script>

          <mx:Panel title="MenuBar Control Example" height="75%" width="75%"
          paddingTop="10" paddingLeft="10">

          <mx:MenuBar labelField="@label" change="menuHandler(event);"
          dataProvider="{menuBarCollection}" />

          </mx:Panel>
          </mx:Application>
          • 2. Re: MenuBar Menu Items Events?
            Level 7
            Hi --

            Thanks for this example. This should really help me out.

            Rich


            "ntsiii" <tspratt@lariatinc.com> wrote in message
            news:e8bnbt$2lk$1@forums.macromedia.com...
            > When the event fires, the event object passed into the handler can be used
            > to
            > access the item that created the menu option. You can use information in
            > the
            > item to decide what action to take. Here is a simple example:
            >
            > Tracy
            >
            > <?xml version="1.0"?>
            > <!-- Simple example to demonstrate the MenuBar control. -->
            > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
            > creationComplete="initCollections();" >
            >
            > <mx:Script>
            > <![CDATA[
            >
            > import mx.events.MenuEvent;
            > import mx.controls.Alert;
            > import mx.collections.*;
            >
            > [Bindable]
            > public var menuBarCollection:XMLListCollection;
            >
            > private var menubarXML:XML =
            > <root>
            > <menuitem label="Menu1">
            > <menuitem label="MenuItem 1-A" data="1A"/>
            > <menuitem label="MenuItem 1-B" data="1B"/>
            > </menuitem>
            > <menuitem label="Menu2">
            > <menuitem label="MenuItem 2-A" type="check"
            > data="2A"/>
            > <menuitem type="separator" />
            > <menuitem label="MenuItem 2-B" >
            > <menuitem label="SubMenuItem 3-A" type="radio"
            > groupName="one" data="3A"/>
            > <menuitem label="SubMenuItem 3-B" type="radio"
            > groupName="one" data="3B"/>
            > </menuitem>
            > </menuitem>
            > </root>;
            >
            > // Event handler for the MenuBar control's change event.
            > private function menuHandler(event:MenuEvent):void
            > {
            > Alert.show("Label: " + event.item.@label + "Data: " +
            > event.item.@data, "Clicked menu item");
            > }
            >
            > // Event handler to initialize the MenuBar control.
            > private function initCollections():void
            > {
            > menuBarCollection = new
            > XMLListCollection(menubarXML.children());
            > }
            > ]]>
            > </mx:Script>
            >
            > <mx:Panel title="MenuBar Control Example" height="75%" width="75%"
            > paddingTop="10" paddingLeft="10">
            >
            > <mx:MenuBar labelField="@label" change="menuHandler(event);"
            > dataProvider="{menuBarCollection}" />
            >
            > </mx:Panel>
            > </mx:Application>
            >
            >