在 vb.net 中如何引发另一个控件的事件 [英] in vb.net how do I raise an event of another control
问题描述
我所见过的每个示例都与鼠标点击有关,然后示例是相同的.
Every sample I've seen that refers to this is about mouse clicks and then the example is the same.
我需要在另一个控件上专门引发一个事件.
I need to specifically raise an event on another control.
我有一个面板,其中包含我这样创建的事件:
I have a panel with an event that I created like this:
Private FlowPanel as new my_FlowLayoutPanel
Addhandler FlowPanel.change, addressof doChange
Public Class my_FlowLayoutPanel
Inherits FlowLayoutPanel
Public Event change(ByVal sender As Object)
Public Const Ver_SCROLL As Integer = &H115
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = Ver_SCROLL Then
RaiseEvent change(Me)
End If
MyBase.WndProc(m)
End Sub
End Class
所以当垂直滚动条移动时,change"事件会触发.
So when the vertical scroll bar moves, the "change" event fires.
现在,我有另一个控件(一个简单的面板),设置如下:
So now, I have another control, (a simple panel) set up like this:
Public Class view_Panel
Inherits System.Windows.Forms.Panel
Protected Overrides Sub WndProc(ByRef m As Message)
Const NCMOUSEMOVE As Integer = &H200
If m.Msg = NCMOUSEMOVE Then
' *** FIRE THE "CHANGE" EVENT ON THE FLOWLAYOUT PANEL
End If
MyBase.WndProc(m)
End Sub
End Class
那么,如何从 view_Panel 触发Change"事件?
So, how do I fire the "Change" event from the view_Panel?
推荐答案
即使阅读了其他答案,例如上面的 'Pouya Samie'(如果可用,请参考 OnChange
),或者这篇更增强的文章"使用反射引发事件" 看起来更简洁但并不总是有效(反映MulticastDelegate
)...
Even after reading other answers such as 'Pouya Samie' above (reflect to OnChange
if available), or this more enhanced article "Raising Events Using Reflection" which seems much cleaner but doesn't always work (reflect to MulticastDelegate
)...
所以终于把我所有的想法都放在了通用方法中,以简单的语法来执行这个任务:
So finally got to put all my ideas in order for a generic method to perform this task with a simple syntax:
TriggerEvent(ComboBox1, "OnSelectedIndexChanged")
请注意,上述方法在 ComboBox1 中是私有不可访问,它甚至未列出在 IntelliSense 列表成员中,但是使用这种反射方法它会起作用好的:
Notice that the above method is private non-accesible inside the ComboBox1, it is even NOT listed on the IntelliSense list members, but with this reflection method it will work OK:
''' <summary>
''' Manually trigger an existing event in a control.
''' </summary>
''' <param name="ctrlObject">The GUI control that that should be operated (such as ComboBox).</param>
''' <param name="onEventName">The OnEvent function regardless of the scope (such as OnSelectedIndexChanged).</param>
''' <returns><code>True</code> when the method is found invoked and returned successfully; <code>false</code> otherwise.</returns>
Public Function TriggerEvent(ctrlObject As Control, onEventName As String) As Boolean
' Get the reference to the method
Dim methodRef As MethodInfo = ctrlObject.GetType().GetMethod(onEventName, _
System.Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static Or _
System.Reflection.BindingFlags.Instance)
If IsNothing(methodRef) Then Return False
' Invoke the method
Try
methodRef.Invoke(ctrlObject, New Object() {EventArgs.Empty})
Return True
Catch ex As Exception
Return False
End Try
End Function
这篇关于在 vb.net 中如何引发另一个控件的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!