我需要将事件作为参数传递给一项功能。 是否有办法这样做?
The reason is that 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, and 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.
举例来说,我要说,我有一部名为“ com”的法典,我有手叫指数ChangedHandler。 在我的法典的几个地方,我有以下两条:
RemoveHandler combobox1.SelectedIndexChanged, AddressOf indexChangedHandler
AddHandler combobox1.SelectedIndexChanged, AddressOf indexChangedHandler
现在,我不想在我的方案上重复上述两条法典(或类似),这样我就想设法这样做:
Private Sub setHandler(evt As Event, hndler As eventhandler)
RemoveHandler evt, hndler
AddHandler evt, hndler
End Sub
因此,在我方案出现这两条法典(或类似)的地方,我只能用:
setHandler(combobox1.SelectedIndexChanged, AddressOf indexChangedHandler)
至今为止,“作为事件”的机组职能部分的争论有误。
P.S:我曾在其他几个论坛问过这个问题,并不断问,为什么我想在撤走之后立即放手。 其原因是,在事件发生时,积极增加一门活动手,导致手臂被处决。 为了避免这种情况,即,为了确保在事件发生后立即执行手稿,我每次想积极补充手稿时,先撤除手稿。
You might be asking why the handler would be added several times in the first place... The reason is because I add the handler only after a particular event, say E1, in my form has occurred (I add the handler within the handler of event E1). And event E1 can occur several times within my form. If I do not remove the handler each time before adding it again, the handler gets added and thus executed several times.
不管怎样,这一职能范围内进行的处理对我来说,目前并不具有最终重要性,而只是将事件作为参数通过的手段。