How do I redirect or avoid WebBrowser Control from Stealing Focus

Posted on 2007-12-21
Medium Priority
Last Modified: 2013-12-25
I am trying to write a program that uses a WebBrowser control to automatically login to a particular website to upload data to the website.  Basically, the UI of the website is poor and permits many input mistakes to happen, so I need to write a program which acts as a frontend to the hidden website.  There is a usernameTextBox, userpasswordTextBox, and loginButton.  I have the autologin functional, but when the main webpage loads, my usernameTextBox loses focus and becomes frustrating to input username and password.
Question by:aflorence07
LVL 19

Expert Comment

by:William Elliott
ID: 20519339
if you have the "name" for the fields, then you shouldn't have to worry about this,..

This is what the HTML tag looks like for the www.google.com search field:

<input maxLength=256 size=55 name=q value="">

Let's say I've got a combo box named cboData

The following code will look for an INPUT tag with name = "q". When it finds it, the current value of the combo box will be put into the search field.

Set docCurrentHTML = MyBrowser.Document.documentElement.All

For Each Item In docCurrentHTML

If Item.tagName = "INPUT" Then
If Item.Name = "q" Then
Item.Value = cboData.Text
Exit For
End If
End If

Next Item

is this what you're referring to?

Accepted Solution

aflorence07 earned 0 total points
ID: 20532699
After researching this problem it is apparent that as a result of using any webpage that uses javascript to redirect focus to a specific element on the page in the WebBrowser control, it will automatically steal the focus from any other control on a Windows Form.

There doesn't seem to be a way to avoid this.  If I programatically try to redirect focus after the redirection it does not do anything.  I found that the only true way around this is to use a lower-level approach of using XHTMLWebRequest Class which I still am having a little difficulty understanding how to use it to accomplish my overall task.

Instead of loading the login page on Form_Load, I just waited to navigate to the login page until after the user has entered their login info and clicked my login button.  So even though I am still using the WebBrowser control method and the same problematic webpage, the user does not experience any issue with stealing of focus prior to them entering their info.  And after the initial sign in, the website no longer uses the javascript redirect so problem is gone :)

Here is a copy of my code I used for a custom control to use for auto login to a site.  Just drop it on a form just like a WebBrowser control.  Use the AutoLogin method to load a loginpage, input specified username and password, and click on a submit button.  The last argument, specifies the webpage that we should be redirected to if login succeeded so we can test if login was successful or not.  That is what the LoginCheck Sub does.  Then one can just code an EventHandler for the SuccessfullLogin Event to test if it was successful or not and decide what they want to do from there.
Public Class AutoNavigatorControl
    Protected Structure LoginStruct
        Dim usernameHTMLelementID As String
        Dim username As String
        Dim userpassHTMLelementID As String
        Dim userpass As String
        Dim submitButtonHTMLelementID As String
    End Structure
    Protected loginInfo As LoginStruct
    Public Event SuccessfullLogin()
    Protected successfullURL As String
    Public Sub AutoLogin(ByVal loginURL As String, ByVal usernameHTMLelementID As String, _
    ByVal username As String, ByVal userpassHTMLelementID As String, _
    ByVal userpass As String, ByVal submitButtonHTMLelementID As String, ByVal loginSuccessfullURL As String)
        With Me
            successfullURL = loginSuccessfullURL
            'store all login credentials
            loginInfo.usernameHTMLelementID = usernameHTMLelementID
            loginInfo.username = username
            loginInfo.userpassHTMLelementID = userpassHTMLelementID
            loginInfo.userpass = userpass
            loginInfo.submitButtonHTMLelementID = submitButtonHTMLelementID
            '/* begin auto login process()
            '//navigate to the page
            '//queue for FormFill
            AddHandler .DocumentCompleted, AddressOf FormFill
            '*/ end auto login process
        End With
    End Sub
    Protected Sub FormFill(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        With Me
            '//fill in username
                .Document.All(loginInfo.usernameHTMLelementID).SetAttribute("value", loginInfo.username)
                '//fill in password
                .Document.All(loginInfo.userpassHTMLelementID).SetAttribute("value", loginInfo.userpass)
                '//press submit button
                'unlink from DocumentCompleted event
                RemoveHandler .DocumentCompleted, AddressOf FormFill
                'queue LoginCheck()
                AddHandler .DocumentCompleted, AddressOf LoginCheck
            End Try
        End With
    End Sub
    Protected Sub LoginCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        With Me
            If .Url.ToString() = successfullURL Then
                RaiseEvent SuccessfullLogin()
            End If
            'unlink from DocumentCompleted Event
            RemoveHandler .DocumentCompleted, AddressOf LoginCheck
        End With
    End Sub
End Class

Open in new window


Expert Comment

ID: 20605331
Closed, 500 points refunded.
EE Admin

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question