Forum Discussion

varghesejim's avatar
varghesejim
Occasional Contributor
11 years ago

WPF Menu Selection

 Out application have a menu.Clicking/hovering over a menu item will bringup child menu items on the right hand side.



I would like to select a menu item from the multi level menu. I am using the following code(Assume we have a menu like Cars->BMW, Bentley, Honda)




   var popuproot,arrayproperties,arraypropertyvalues, menuitem, anotheritem;


 


   popuproot = GetPopuproot();

   popuproot.Refresh();



    arrayproperties=Array("ClrClassName","Header.DisplayName","ClrFullClassName");


   arraypropertyvalues=Array("XamMenuItem","Cars","Infragistics.Controls.Menus.XamMenuItem");


 


   menuitem= popuproot.FindChild(arrayproperties,arraypropertyvalues,20); 


   if( menuitem.Enabled==true)


   {


       menuitem.Click();


       Delay(100);

        arraypropertyvalues=Array("XamMenuItem","BMW","Infragistics.Controls.Menus.XamMenuItem");


        anotheritem = menuitem.FindChild(arrayproperties,arraypropertyvalues,20);


        if( anotheritem.Enabled==true)


        {


            anotheritem.Click();


         }

}



I can see the the list of cars as a result of the first click(). However I am not able to click  on "BMW". It fails saying that object does not exist(anotheritem)



Any help is appreciated

  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Likely the menu is changing or closing as the mouse leaves the menu area.

    We experienced a similar issue when attempting to click a menu item in the file menu of our application. Because it was the first item in the list the mouse had to move from it's current position over File and cross over Edit. This meant that the Edit menu now opened up and the File menu closed, thus the menu item no longer existed.



    You can attempt to click the lower right edge of the menu item. This usually helps to keep the mouse from traveling over other items during the click() method.



    Edit: See code comments






    var popuproot,arrayproperties,arraypropertyvalues, menuitem, anotheritem; 


    popuproot = GetPopuproot();


    popuproot.Refresh();


    //searching for enabled items only


    arrayproperties= ["Enabled","ClrClassName","Header.DisplayName","ClrFullClassName"];


    arraypropertyvalues= [true,"XamMenuItem","Cars","Infragistics.Controls.Menus.XamMenuItem"];


    menuitem = popuproot.FindChild(arrayproperties,arraypropertyvalues,20);


    //clicking the bottom right edge of the menuitem to ensure mouse position is closest to the next possible flyout menu/menuitem


    menuitem.Click(menuitem.width - 1, menuitem.height - 1);


    Delay(100,"Waiting for menu to open...");


    arraypropertyvalues= [true,"XamMenuItem","BMW","Infragistics.Controls.Menus.XamMenuItem"];

    //maybe need to call GetPopuproot(); to get the correct parent object here


    anotheritem = menuitem.FindChild(arrayproperties,arraypropertyvalues,20); //also make sure the BMW item is actually a child of the menuitem!!!!


    anotheritem.Click(anotheritem.width - 1, anotheritem.height - 1);