Question for eventhandler on webbrowser element

A

Andre Rode

Hi,

since I solved my last problem with the help of this ng, I've got new
stuff to solve :)
First: I'm new to creating own events, until now I only got to work with
pre-defined events.

I'm using a virtual webbrowser, this means there's no form containing a
webbrowser element. I want to run this within a DLL.
But I'm not able to get the event Document_completed working. When the
browser.navigate is called, he never jumps into document_completed. It
seems I just declared the event in a wrong way.
doSomething is the sub called from outside the dll.

Here's the code:


Public Class myclass
Dim mybrowser As Windows.Forms.WebBrowser

Public Event mbDocumentCompleted As & _
Windows.Forms.WebBrowserDocumentCompletedEventHandler

Public Sub doSomething()
mybrowser = New Windows.Forms.WebBrowser
AddHandler mybrowser.DocumentCompleted, AddressOf
Document_Completed
[some code]
mybrowser.Navigate("https://www.somesite.com")
end sub

Private Sub Document_Completed() Handles Me.mbDocumentCompleted
[This should happen]
End Sub
End Class

Hope you can help me,
greetings from Germany,
André
 
K

kimiraikkonen

Hi,

since I solved my last problem with the help of this ng, I've got new
stuff to solve :)
First: I'm new to creating own events, until now I only got to work with
pre-defined events.

I'm using a virtual webbrowser, this means there's no form containing a
webbrowser element. I want to run this within a DLL.
But I'm not able to get the event Document_completed working. When the
browser.navigate is called, he never jumps into document_completed. It
seems I just declared the event in a wrong way.
doSomething is the sub called from outside the dll.

Here's the code:

Public Class myclass
Dim mybrowser As Windows.Forms.WebBrowser

Public Event mbDocumentCompleted As & _
Windows.Forms.WebBrowserDocumentCompletedEventHandler

Public Sub doSomething()
mybrowser = New Windows.Forms.WebBrowser
AddHandler mybrowser.DocumentCompleted, AddressOf
Document_Completed
[some code]
mybrowser.Navigate("https://www.somesite.com")
end sub

Private Sub Document_Completed() Handles Me.mbDocumentCompleted
[This should happen]
End Sub
End Class

Hope you can help me,
greetings from Germany,
André

Why don't you just do:

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles
WebBrowser1.DocumentCompleted

' Code to be executed...

End Sub
 
A

Andre Rode

Why don't you just do:

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles
WebBrowser1.DocumentCompleted

' Code to be executed...

End Sub

Hi

cause the webbrowser element created in the main sub doesn't have any
events. That's my problem, I need to assign this event to the webbrowser
object.
 
T

Thorsten Doerfler

Andre said:
I'm using a virtual webbrowser, this means there's no form containing a
webbrowser element. I want to run this within a DLL.
But I'm not able to get the event Document_completed working. When the
browser.navigate is called, he never jumps into document_completed.

It's no wonder the posted code didn't even compile.
Here's the code:


Public Class myclass
Dim mybrowser As Windows.Forms.WebBrowser

Here is your choice to declare WithEvents, so the IDE/Compiler will do
the event stuff for you and you can select the events from the list,
as you know it from Forms.
Public Event mbDocumentCompleted As & _
Windows.Forms.WebBrowserDocumentCompletedEventHandler

Amazing!

'Event' declares own events for your class. For handling events, this
ist not necessary.
Public Sub doSomething()
mybrowser = New Windows.Forms.WebBrowser
AddHandler mybrowser.DocumentCompleted, AddressOf
Document_Completed
[some code]
mybrowser.Navigate("https://www.somesite.com")
end sub

Private Sub Document_Completed() Handles Me.mbDocumentCompleted

Handles is used with WithEvents.
[This should happen]
End Sub
End Class

An working example:

Public Class Form1

Private m_WB As New OwnWebbrowser

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click

m_WB.Navigate()
End Sub
End Class

Public Class OwnWebbrowser
Dim m_WebBrowser As WebBrowser

Public Sub New()
m_WebBrowser = New WebBrowser

AddHandler m_WebBrowser.DocumentCompleted, _
AddressOf Document_Completed
End Sub

Public Sub Navigate()
m_WebBrowser.Navigate("http://www.vb-hellfire.de/")
End Sub

Private Sub Document_Completed(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs)

Debug.Print(e.Url.ToString)
End Sub
End Class

Thorsten Doerfler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top