问题描述
公开事件DocumentCompleted作为WebBrowserDocumentCompletedEventHandlerDim arg()As Object = {homeTeam,guestTeam}
AddHandler browser.DocumentCompleted,New
WebBrowserDocumentCompletedEventHandler(AddressOf DoStuff)
Private Sub DoStuff(ByVal sender As Object,ByVal e As WebBrowserDocumentCompletedEventArgs)
End Sub
/ pre>
在触发DocumentCompleted事件时,如何通过homeTeam和guestTeam。
我想把上面的值放在Dostuff方法里面。
请帮助。
首先,你不能把它挂在无处的中间位置:
Dim arg()As Object = {homeTeam,guestTeam}
AddHandler browser.DocumentCompleted,
新建WebBrowserDocumentCompletedEventHandler(AddressOf DoStuff)
AddHandler
可能需要在一些初始化
方法中,这可能在 Sub New
, InitializeComponent
之后,或 Form_Load
内,或一旦你期望它被触发(在特定事件之后)。请注意,您正在使用默认事件类型的本机.NET组件的默认事件。在这种情况下,触发时,您不能直接使用它已经提供的任何东西。请参阅
然而,您可以覆盖所有相关类,并拥有自己的MyWebBrowser控件和您自己的事件,其中包含其他属性。见下面的例子:
公共类Form1
Sub New()
'设计师。
InitializeComponent()
Dim浏览器作为新的MyWebBrowser
AddHandler browser.MyDocumentCompleted,AddressOf DoStuff
End Sub
Private Sub DoStuff(ByVal发件人As Object,ByVal e As MyWebBrowserDocumentCompletedArgs)
Dim guestTeam As String = e.GuestTeam'guest team
Dim homeTeam As String = e.HomeTeam'和主队都可以访问
',所以你可以对它们进行一些处理
End Sub
公共类MyWebBrowserDocumentCompletedArgs:Inherits WebBrowserDocumentCompletedEventArgs
Dim _homeTeam As String
Dim _guestTeam As String
公共ReadOnly属性HomeTeam
获取
返回_homeTeam
结束获取
结束属性
公共ReadOnly属性GuestTeam
获取
返回_guestTeam
结束获取
End Property
Sub New(url As Uri,homeTeam As String,guestTeam As String)
MyBase。 New(url)
_homeTeam = homeTeam
_guestTeam = guestTeam
End Sub
结束类
公共类MyWebBrowser:继承WebBrowser
公共代理Sub MyWebBrowserDocumentCompletedEventHandler(e As MyWebBrowserDocumentCompletedArgs)
公共事件MyDocumentCompleted作为MyWebBrowserDocumentCompletedEventHandler
受保护的覆盖Sub OnDocumentCompleted(e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
MyBase.OnDocumentCompleted(e)
'homeTeam和guestTeam需要从当前的MyWebBrowser实例中提取,并进一步传递
RaiseEvent MyDocumentCompleted(New MyWebBrowserDocumentCompletedArgs(e.Url,homeTeam,guestTeam))
End Sub
结束类
结束类
如果您的项目相对较小,您可以确实有那些作为全局变量,如@Vlad在评论中建议的。
Public Event DocumentCompleted As WebBrowserDocumentCompletedEventHandler
Dim arg() As Object = {homeTeam, guestTeam}
AddHandler browser.DocumentCompleted, New
WebBrowserDocumentCompletedEventHandler(AddressOf DoStuff)
Private Sub DoStuff(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
End Sub
How can I pass the homeTeam and guestTeam when firing the DocumentCompleted event.I want to ge the above to values to inside the Dostuff method.
Please help.
First of all, you cannot have this hanging in the middle of nowhere:
Dim arg() As Object = {homeTeam, guestTeam}
AddHandler browser.DocumentCompleted,
New WebBrowserDocumentCompletedEventHandler(AddressOf DoStuff)
AddHandler
probably needs to be in some Initialize
method, which could be inside Sub New
, after InitializeComponent
, or inside Form_Load
, or as soon as you expect it to be triggered (after a specific event). Notice here that you are using a default event of a native .NET component, with a default event type. In this case you cannot directly consume anything other than what it already provides, when triggered. See WebBrowser.DocumentCompleted Event on MSDN.
You can, however, override all relevant classes and have your own MyWebBrowser control and your own event, with would contain additional properties. See below example:
Public Class Form1
Sub New()
' This call is required by the designer.
InitializeComponent()
Dim browser As New MyWebBrowser
AddHandler browser.MyDocumentCompleted, AddressOf DoStuff
End Sub
Private Sub DoStuff(ByVal sender As Object, ByVal e As MyWebBrowserDocumentCompletedArgs)
Dim guestTeam As String = e.GuestTeam 'guest team
Dim homeTeam As String = e.HomeTeam 'and home team are both accessible
'so you can do some processing on them
End Sub
Public Class MyWebBrowserDocumentCompletedArgs : Inherits WebBrowserDocumentCompletedEventArgs
Dim _homeTeam As String
Dim _guestTeam As String
Public ReadOnly Property HomeTeam
Get
Return _homeTeam
End Get
End Property
Public ReadOnly Property GuestTeam
Get
Return _guestTeam
End Get
End Property
Sub New(url As Uri, homeTeam As String, guestTeam As String)
MyBase.New(url)
_homeTeam = homeTeam
_guestTeam = guestTeam
End Sub
End Class
Public Class MyWebBrowser : Inherits WebBrowser
Public Delegate Sub MyWebBrowserDocumentCompletedEventHandler(e As MyWebBrowserDocumentCompletedArgs)
Public Event MyDocumentCompleted As MyWebBrowserDocumentCompletedEventHandler
Protected Overrides Sub OnDocumentCompleted(e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
MyBase.OnDocumentCompleted(e)
'homeTeam and guestTeam need to be extracted from the current instance of MyWebBrowser, and passed further
RaiseEvent MyDocumentCompleted(New MyWebBrowserDocumentCompletedArgs(e.Url, "homeTeam", "guestTeam"))
End Sub
End Class
End Class
If your project is relatively small, you can indeed have those as global variables, as @Vlad suggested in the comments.
这篇关于调用和事件时如何传递其他参数VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!