2011-11-10 10 views
1

我只是有一個關於將一個目標方法作爲變量參數添加到我的調用方法的簡短問題。使用方法參數來路由我的事件

我想發送一個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

取代低於問號這是你正在嘗試做什麼?

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 

在這個例子中,我改變了每次出於說明目的而改變文字時發生的事情。

+0

當'TextBox1_TextChanged'被其中一個動作的ARGUMENT調用時,我想調用'act1'或'act2'或'act3'。在參數'e As Action'中應該是'act?'之一,然後我可以調用'actX'的方法,它除了方法描述外沒有任何接口。 – Nasenbaer