How do I bring Set Focus of MDI Child Window using UIAutomation

Posted by Scott Ferguson on Stack Overflow See other posts from Stack Overflow or by Scott Ferguson
Published on 2010-04-26T04:13:36Z Indexed on 2010/04/26 4:23 UTC
Read the original article Hit count: 345

Filed under:
|

We have an old legacy application we need to automate. It uses MDI Windows.

We're using UIAutomation and I can succesfully get the appropriate AutomationElement for each MDI Child window. What I cannot do is bring that element into focus.

Here is some example code that I tried, that fails:

        var desktop = AutomationElement.RootElement;
        var dolphin = desktop.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty,
                    "Dolphin for Windows",
                    PropertyConditionFlags.IgnoreCase));
        dolphin.SetFocus();

        var workspace = dolphin.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty,
                    "Workspace",
                    PropertyConditionFlags.None));

        var childWindow = workspace.FindFirst(TreeScope.Children, new
                PropertyCondition(AutomationElement.NameProperty, "Sharp   "));
        childWindow.SetFocus();

The last line in this code fails with System.InvalidOperationException

Experimenting, I tried finding a control on the childWindow, and calling SetFocus on it. It DID correctly set the focus on the right control, but it did not bring the MDI window to the foreground.

Any ideas?

© Stack Overflow or respective owner

Related posts about c#

Related posts about uiautomation