Activate menu item without sendkey or mouse movement

Class library usage, coding and language questions.
tsmith
Posts: 1
Joined: Wed Jul 11, 2007 10:26 pm

Activate menu item without sendkey or mouse movement

Post by tsmith » Wed Jul 11, 2007 10:31 pm

.Net 2005, C#, v1.2

I am trying to get a handle on a menu item element I want, and then tell it to DoDefaultAction()

By doing ...Element.GetChild(2).GetChild(1).DoDefaultAction I can open the menu -- this works fine.

Before I open the menu, I can find the subitem I want to select by doing ...Element.GetChild(2).GetChild(1).GetChild(0).GetChild(12) -- but I cannot DoDefaultAction (I assume because menu is not open, this element's state is Unavailable)

After the menu is open, though, the last child element has only one child, not the 19 that it should! If I look inside that child, it has no children at all... where did my menu items go?

If I'm going about this entirely the wrong way, let me know... I need it to be without sendkeys or mouse movement, but activate the menu item.

Thanks!

admin
Site Admin
Site Admin
Posts: 350
Joined: Wed Jul 05, 2006 7:44 pm

Post by admin » Thu Jul 12, 2007 8:37 pm

I need it to be without sendkeys or mouse movement, but activate the menu item.
Please try the following:
(This code works with the VS2005Application sample)

Code: Select all

Control menuStrip = form.FindControlName("menuStrip1");

Element menuItem = menuStrip.Element.FindChild(Role.MenuItem, "Submenu2");
if (menuItem == null)
    return 1;
menuItem.DoDefaultAction();
Application.Sleep(500);

menuItem = menuItem.FindChild(Role.MenuItem, "Submenu2Item3");
if (menuItem == null)
    return 1;
menuItem.DoDefaultAction();
Application.Sleep(500);

menuItem = menuItem.FindChild(Role.MenuItem, "Subitem13");
if (menuItem == null)
    return 1;
menuItem.DoDefaultAction();
Application.Sleep(500);

menuItem = menuItem.FindChild(Role.MenuItem, "Subitem31");
if (menuItem == null)
    return 1;
menuItem.DoDefaultAction();
Jenö
Ranorex Team