How do I redirect or avoid WebBrowser Control from Stealing Focus

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.
aflorence07Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

William ElliottSr Tech GuruCommented:
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?
http://www.vbforums.com/archive/index.php/t-160521.html
0
aflorence07Author Commented:
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
            .Navigate(loginURL)
            '//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
            Try
 
                .Document.All(loginInfo.usernameHTMLelementID).SetAttribute("value", loginInfo.username)
 
                '//fill in password
 
                .Document.All(loginInfo.userpassHTMLelementID).SetAttribute("value", loginInfo.userpass)
 
                '//press submit button
                .Document.All(loginInfo.submitButtonHTMLelementID).InvokeMember("click")
 
                'unlink from DocumentCompleted event
                RemoveHandler .DocumentCompleted, AddressOf FormFill
 
                'queue LoginCheck()
                AddHandler .DocumentCompleted, AddressOf LoginCheck
            Catch
 
            End Try
 
        End With
 
    End Sub
 
    Protected Sub LoginCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
 
        With Me
            Debug.WriteLine(.Url.ToString)
            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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Computer101Commented:
Closed, 500 points refunded.
Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.