使用方法参数路由我的事件

时间:2011-11-10 10:48:02

标签: .net vb.net

我只是有一个关于将目标方法作为变量参数添加到我的调用方法中的简短问题。

我想将TextChanged事件发送到特殊文本框。但是我希望在我的方法中有一个“变量”来将处理程序添加到文本框中。因为可以更改文本框,然后我可以更改应该将“已更改的事件”路由到的处理程序。

我应该用???

替换下面的问号
Dim TextBox1 as TextBox
Dim TextBox2 as TextBox

Private Sub DoIt
Call TestRouting(TextBox1, TextBox_TextChanged)'I want to submit the methode where the changed event should route to
End Sub

Private Sub TestRouting(byval Obj as TextBox, byval ChangedAction as ???)
   Addhandler Obj.TextChanged, AddessOf ChangedAction
End sub

Private sub TextBox_TextChanged(byval sender as object, byval e as args)
'do something
End Sub

1 个答案:

答案 0 :(得分:1)

这是你想要做的吗?

Dim someAction As New Action(AddressOf act1)
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    someAction()
End Sub

Private Sub act1()
    Debug.WriteLine("act1")
    someAction = New Action(AddressOf act2)
End Sub

Private Sub act2()
    Debug.WriteLine("act2")
    someAction = New Action(AddressOf act3)
End Sub

Private Sub act3()
    Debug.WriteLine("act3")
    someAction = New Action(AddressOf act1)
End Sub

在这个例子中,我更改了每次文本更改时发生的事情,以便进行说明。