将值从curElement传递给另一个私有子

时间:2014-11-13 08:58:21

标签: vb.net

嗨我在Private Sub中有For Each,我想将cuElement的值传递给另一个Private sub。

Dim theAnchorsCollection As HtmlElementCollection = Nothing
theAnchorsCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each curElement As HtmlElement In theAnchorsCollection
            curElement.Style = "style code here"
            AddHandler curElement.Click, AddressOf OnaClick
Next

我想将值传递给另一个子:

 Private Sub OnaClick(sender As Object, e As HtmlElementEventArgs)
    Dim endereco As String
    endereco = curElement.GetAttribute("href").ToString()
    'Do other things
End Sub

我想知道在这个子句中我是否可以使用某些东西来获得其他元素的价值。

1 个答案:

答案 0 :(得分:0)

没关系,我找到了答案(发件人)我想要的变量。

正如我在MSDN论坛中找到的那样

当您使用Addhandler订阅给定事件时,您将事件的必需参数传递给具有委托的AddressOf的过程,其参数签名"必须"与事件相同。

演示:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Button1.MouseDown, AddressOf btn_MouseDown
    End Sub

    Sub btn_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs)
        MsgBox(e.Button.ToString)
    End Sub
End Class

使用Addhandler,我们重新订阅按钮的MouseDown事件,并指定将在后面调用的过程。传递的参数是object(sender)和MouseEventArgs(e),您可以在上面的事件子中使用它。 (例如:要使鼠标按钮失效。)