将事件作为参数传递 [英] Passing an event as a parameter

查看:59
本文介绍了将事件作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要将一个事件作为参数传递给一个函数,我想知道是否有任何方法可以做到这一点。



之所以我需要这样做,是因为我的程序遍布两行代码,我动态删除处理程序一个事件,然后再次设置处理程序。我正在为几个不同的事件和事件处理程序执行此操作,因此我决定编写一个执行此操作的函数。



例如,假设我的代码中有一个名为combobox1的组合框,我有一个名为indexChangedHandler的处理程序。在我的代码的几个地方,我有以下两行:



Hi all,

I need to pass an event as a parameter to a function and I'm wondering if there is any way of doing this.

The reason why I need to do this is because I have a sequence of two lines of code that is littered all over my program, where I dynamically remove the handler to an event, then set the handler again. I'm doing this for several different events and event handlers, so I've decided to write a function that does this.

As an example, let's say I have a combobox in my code called combobox1, and I have the handler called indexChangedHandler. In several places of my code, I have the following two lines:

<br />
RemoveHandler combobox1.SelectedIndexChanged, AddressOf indexChangedHandler<br />
AddHandler combobox1.SelectedIndexChanged, AddressOf indexChangedHandler<br />





现在,我不知道我想继续在我的程序中重复上面两行代码(或类似代码),所以我正在寻找一种方法:





Now, I don't want to keep on repeating the above two lines of code (or similar) all over my program, so I'm looking for a way to do this:

<br />
 Private Sub setHandler(evt As Event, hndler As eventhandler)<br />
        RemoveHandler evt, hndler<br />
        AddHandler evt, hndler<br />
 End Sub<br />





以便那两行代码(或类似代码)无处不在发生在我的程序中,我可以用以下代码替换它们:





so that everywhere where those two lines of code(or similar) occur in my program, I can just replace them with:

<br />
 setHandler(combobox1.SelectedIndexChanged, AddressOf indexChangedHandler)<br />





到目前为止,evt as事件setHandler函数的参数的一部分给出了错误。任何关于正确方法的想法都将受到欢迎。谢谢。



So far, the "evt as Event" part of the argument of the setHandler function is giving an error. Any ideas on the right way to do this will be very welcomed. Thanks.

推荐答案

你可以使用反射。



You can use reflection.

Imports System.Reflection

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Me.setHandler("Click", CType(AddressOf test, EventHandler), Me.Button1)
        Me.setHandler("Click", CType(AddressOf test, EventHandler), Me.Button1)
        Me.setHandler("Click", CType(AddressOf test, EventHandler), Me.Button1)
    End Sub

    Sub setHandler(eventName As String, handler As [Delegate], o As Object)
        Dim eInfo As EventInfo = o.GetType.GetEvent(eventName, BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.IgnoreCase)
        eInfo.RemoveEventHandler(o, handler)
        eInfo.AddEventHandler(o, handler)
    End Sub

    Sub test(sender As Object, e As EventArgs)
        MsgBox("Ok")
    End Sub

End Class


此代码没有用处:

There is no use for this code:
Private Sub setHandler(evt As Event, hndler As eventhandler)
RemoveHandler evt, hndler
AddHandler evt, hndler
End Sub





您删除然后添加相同的处理程序。这样做没用。它与此代码有些相同:





You remove and then add the same handler. It's useless to do this. It's somewhat the same as this code:

temp = a
a = 100
a = temp





也许解释一下你想要达到的目标,这样我就可以给你一个解决方案:-D



祝你好运!



Maybe explain what you want to achieve so I can maybe give you a solution for that :-D

Good luck!


这篇关于将事件作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆