Passing aruments to the DocumentCompleted event of a WebBrowser control

Hi Experts,

How to I pass an argument to the DocumentCompleted event of a WebBrowser control. The DocumentComplete event is called automatically so I do not get to write the values I want to pass into the event arg list. Thanks.
DColinAsked:
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.

Slow_moCommented:
What and why do you need to pass? DocumentCompleted event is raised when control finishes loading a document. I don't quite understand, what data do you need as an event argument.
0
kblauCommented:
Attached code shows a new class derived from WebBrowser that creates a new a event and new event arguments that allow data to be passed to the handler.  The new event can be set from the properties window on the design form.  You can set the property otherData at anytime.

Imports System.ComponentModel
 
Public Class BrowserSub
    Inherits WebBrowser
 
    Public Delegate Sub WebBrowserDocumentCompletedEventHandlerInSub(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedInSubEventArgs)
 
    Public Event DocumentCompletedInSub As WebBrowserDocumentCompletedEventHandlerInSub
 
    Protected Overrides Sub OnDocumentCompleted(ByVal e As WebBrowserDocumentCompletedEventArgs)
 
        Dim e_sub As New WebBrowserDocumentCompletedInSubEventArgs(e.Url, OtherData)
        RaiseEvent DocumentCompletedInSub(Me, e_sub)
 
        MyBase.OnDocumentCompleted(e)
    End Sub
 
    Private _otherData As Object
    <Bindable(True)> _
    Public Property OtherData() As Object
 
        Set(ByVal value As Object)
            _otherData = value
        End Set
 
        Get
            Return _otherData
        End Get
    End Property
 
End Class
Public Class WebBrowserDocumentCompletedInSubEventArgs
    Inherits EventArgs
 
    ' Methods
    Public Sub New(ByVal url As Uri, ByVal otherData As Object)
        _url = url
        _otherData = otherData
    End Sub
 
    ' Properties
    ReadOnly Property Url() As Uri
        Get
            Return _url
        End Get
 
    End Property
 
    ReadOnly Property OtherData() As Object
        Get
            Return _otherData
        End Get
 
    End Property
 
    ' Fields
    Private _url As Uri
    Private _otherData As Object
 
End Class
 
Public Class Form1
 
    
 
    Private Sub BrowserSub1_DocumentCompletedInSub(ByVal sender As System.Object, ByVal e As WindowsApplication5.WebBrowserDocumentCompletedInSubEventArgs) Handles BrowserSub1.DocumentCompletedInSub
        Dim thisIsOtherData As String = e.OtherData
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BrowserSub1.OtherData = "My other data"
    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
kblauCommented:
What I posted adds another event where you can add additional event arguments.  I agree with slow mo that this probably does not buy you anything.  Kudos for being observant about the event firing in code that we have no control over even with overriding.  You can check out google: download reflector vb.net c#

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

kblauCommented:
Please note that an Event is just a keyword that sets t he scope of a delegate to be more secure - see Juval Lowy Components book
0
DColinAuthor Commented:
Hi Slow mo,

I want to do something like the attached code. When the page has loaded auto fill the email address in a form, but how do I pass the Email address string to the DocumentCompleted event. Thanks.
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
 
   Dim Email as string
        'WebBrowser1.Document.GetElementsByTagName("input").GetElementsByName("emailaddress").Item(0).SetAttribute("value", Email)
 
    End Sub

Open in new window

0
Slow_moCommented:
So if I understand you correctly, you need to fill the textbox on your windows form based on the data in web page. Why do you need data in web page to be passed as an event argument? Can't you just access it from the WebBrowser1 object after the DocumentCompleted event is raised?

Anyway, if you really need it as an argument, I would create a wrapper class for WebBrowser object that would have a custom event with a data you need. In this case your class catches DocumentCompleted event, retreives an e-mail from the web page and raises event with this e-mail as an argument.
0
icrCommented:
I *think* they have an Email string and, when the webpage loads, set the value of a textbox inside the webpage to that email. Where is the Email coming from? Can't you just use a local variable?
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.NET

From novice to tech pro — start learning today.