Skip to main content

 none
ISelectionItemProvider not working for Windows Forms MenuItem? RRS feed

  • Question

  • Hi,

    I am using UIAutomation to do testing on a Windows Forms application. However apparently, contrary to what's stated here , I can't use element.GetCurrentPattern(SelectionItemPattern.Pattern) on a MenuItem, it throws InvalidOperationException: "Unsupported pattern"

    Is there any solution for this? Or did I use the wrong method?

    Wednesday, January 5, 2011 9:55 AM

Answers

  • I'm not sure what a 'selectableMenuItem' is. 

    I looked up MenuItem in MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem.aspx

    There's no property called 'selectable'.  There's a property called Checked - I'm assuming that is what you mean.

    In managed-client UIA, I don't know of a way to do what you are asking.  In the COM client for UIA, which was introduced in Win7 (but is available on Vista and XP, too), you can get GetCurrentPattern to ask for the ILegacyIAccessible pattern, which is a way to drill into the underlying MSAA implementation for MSAA controls. 

    On that interface, you can call get_CurrentState to get the MSAA state, and STATE_SYSTEM_CHECKED will be set if the menu item is checked.

    Thanks,
    Michael


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, January 14, 2011 5:21 PM

All replies

  • Hi, Louis,

    Windows Forms doesn't support UI Automation (it pre-dates UI Automation by several years), so the UI Automation support for Windorms Forms is done through the "MSAA-to-UIA Proxy", which tries its best to translate MSAA to UIA.  There's a good discussion on this here.  However, not all control patterns are supported through the MSAA-to-UIA Proxy, and in this case, SelectionItem pattern is not supported.  (Please see the full list here.)  You should be able to use the Invoke Pattern to activate Windows Forms menu items.

    Thanks,
    Michael


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, January 5, 2011 9:22 PM
  • Is there a way to check whether a selectable MenuItem is selected or not?
    • Edited by Louis Rhys Monday, January 17, 2011 2:12 AM
    Thursday, January 6, 2011 3:23 AM
  • I'm not sure what a 'selectableMenuItem' is. 

    I looked up MenuItem in MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem.aspx

    There's no property called 'selectable'.  There's a property called Checked - I'm assuming that is what you mean.

    In managed-client UIA, I don't know of a way to do what you are asking.  In the COM client for UIA, which was introduced in Win7 (but is available on Vista and XP, too), you can get GetCurrentPattern to ask for the ILegacyIAccessible pattern, which is a way to drill into the underlying MSAA implementation for MSAA controls. 

    On that interface, you can call get_CurrentState to get the MSAA state, and STATE_SYSTEM_CHECKED will be set if the menu item is checked.

    Thanks,
    Michael


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, January 14, 2011 5:21 PM
  • You're right, What I meant was the Checked property of MenuItem.


    What do you mean by the COM client for UIA? How do I access it from a .NET managed code?

    Monday, January 17, 2011 2:39 AM
  • Hi, Louis,

    Here's the MSDN intro for the UI Automation client interfaces:
    http://msdn.microsoft.com/en-us/library/ee671216(v=VS.85).aspx

    You can use it through managed interop - you can run tlbimp.exe on UIAutomationCore.dll (in the System32 directory) to create an interop library. 

    Thanks,
    Michael


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, January 17, 2011 4:22 PM
  • Oh dear that sounds scary, ha ha

    Thanks! I'll take a look.

    Tuesday, January 18, 2011 7:09 AM
  • It's not all that scary.  My blog post on custom patterns (http://blogs.msdn.com/b/winuiautomation/archive/2010/12/08/uia-custom-patterns-part-1.aspx) includes a unit test that does exactly this.

    Thanks,
    Michael


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, January 28, 2011 5:24 PM