Search code examples
c#.netwpfinternet-explorerwatin

How to set focus to browser window using WatiN


I'm trying open new IE window with WatiN from WPF application. I want to focus on the IE window. When I start project in debug mode it works correctly. But when I start without debugging new browser window not focused. Have any ideas?

Code for opening browser:

using (var browser = new IE(someAddress))
{
    ...
}

Solution

  • I stumbled upon this solution a while back by looking through the methods brought up by Visual Studio's Intellisense for the IE class.

    using (var browser = new IE(someAddress))
    {
        browser.BringToFront();
    
        // ... Your other code
    }
    

    The IE#BringToFront() method essentially sets focus to the window. Another common task is maximizing the window as well:

    using (var browser = new IE(someAddress))
    {
        // using WatiN.Core.Native.Windows.NativeMethods;
        browser.ShowWindow(WindowShowStyle.Maximize);
        browser.BringToFront();
    
        // ... Your other code
    }