2 Replies Latest reply on Oct 6, 2010 4:33 PM by aman_thind

    Adding custom context menu items on right click in textbox in AIR

    aman_thind

      Hi,

       

      I want to add a custom context menu on the right click of a Text Input component.

       

      At the moment, when I right click on the textInput, it only shows the default edit context menu items and doesn’t shows up the custom items I added in there.

      On the other hand, if I add the custom context menu items to any other UI Component, where AIR doesn’t contains the default edit context menu, then it shows up the custom items properly in there.

       

      Just wondering, if I am missing something here ?

       

      Here is the code snippet I am using :-

       

      private function addContextMenuToTarget() : void

            {

               var item : ContextMenuItem = new ContextMenuItem( “Custom Item 1” );

              

               if( !target.contextMenu )

               {

                  target.contextMenu = new ContextMenu();

                  target.contextMenu.items = new Array();

               }

               target.contextMenu.items.push( item );

            }

       

      Here ‘target’ is the textInput, where I need to add the custom context menu items on right click.

       

      Many Thanks,

      Aman

        • 1. Re: Adding custom context menu items on right click in textbox in AIR
          nataliehuynh

          Hi Aman,


          For the mx:TextInput, you need to extract the internal TextField and apply the customMenu to that instead. It will remove your default values and apply the custom ones. Example below:


          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="addContextMenuToTarget();">
              <mx:Script>
                  <![CDATA[
                      import flash.ui.ContextMenu;
                      import flash.ui.ContextMenuItem;
                     
                      private function addContextMenuToTarget() : void
                      {
                          stage.nativeWindow.activate();
                          var myContextMenu:ContextMenu = new ContextMenu();
                          var item1:ContextMenuItem = new ContextMenuItem("Test 1");
                          var item2:ContextMenuItem = new ContextMenuItem("Test 2");
                          myContextMenu.customItems.push(item1, item2);
                          var txt:TextField = target.mx_internal::getTextField() as TextField;                
                          txt.contextMenu = myContextMenu;
                      }
                  ]]>
          </mx:Script>
          <mx:TextInput id="target" text="testing"/>
          </mx:Application>

           

          Please let me know if that works out for you.

           

          Thanks,

          Natalie

          • 2. Re: Adding custom context menu items on right click in textbox in AIR
            aman_thind Level 1

            Thanks for the response Natalie. Yes I ended up doing exactly that.

             

            Best Regards,

            Aman