gpt4 book ai didi

vb.net - 如何在调用和事件 VB.net 时传递附加参数

转载 作者:行者123 更新时间:2023-12-02 00:54:10 28 4
gpt4 key购买 nike

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

在触发 DocumentCompleted 事件时如何传递 homeTeam 和 guestTeam。我想将上面的值放入 Dostuff 方法中。

请帮忙。

最佳答案

首先,你不能把它卡在不知名的地方:

Dim arg() As Object = {homeTeam, guestTeam}
AddHandler browser.DocumentCompleted,
New WebBrowserDocumentCompletedEventHandler(AddressOf DoStuff)

AddHandler 可能需要位于某个 Initialize 方法中,该方法可能位于 Sub New 内,位于 InitializeComponent 之后,或在 Form_Load 内,或在您期望触发时(在特定事件之后)立即触发。请注意,此处您使用的是 native .NET 组件的默认事件和默认事件类型。在这种情况下,当触发时,您不能直接使用它已经提供的内容之外的任何内容。请参阅WebBrowser.DocumentCompleted Event on MSDN .

但是,您可以覆盖所有相关类并拥有自己的 MyWebBrowser 控件和自己的事件,其中将包含其他属性。请参阅下面的示例:

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

如果您的项目相对较小,您确实可以将它们作为全局变量,正如 @Vlad 在评论中建议的那样。

关于vb.net - 如何在调用和事件 VB.net 时传递附加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320634/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com