我需要向TableLayoutPanel中的MyButton添加一个事件处理程序。我已经查看了Stackoverflow和其他几个视线,我没有得到它。我所拥有的是一个从TableLayoutPanel获取控件的函数。
Public Function AddEventHandler(ByVal iColumnIndex As Integer, ByVal iRowIndex As Integer, ByVal eh As Action(Of EventHandler)) As Boolean
Dim bReturn As Boolean = False
m_TblLoPnl.GetControlFromPosition(iColumnIndex, iRowIndex)
Return bReturn
End Function
但无法找到我需要做的事情来为MyButton.Click事件分配一个事件处理程序。 MyButton继承了Button类,我知道它有一个click事件。
这是我到目前为止通过AddEventHandler
传递函数的内容Public Function AddEventHandler(ByVal iColumnIndex As Integer, ByVal iRowIndex As Integer, ByRef func As Object) As Boolean
Dim bReturn As Boolean = False
m_TmpBtn = m_TblLoPnl.GetControlFromPosition(iColumnIndex, iRowIndex)
AddEventHandler(m_TmpBtn.Click, AddressOf func)
Return bReturn
End Function
Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
答案 0 :(得分:0)
假设GetControlFromPosition返回一个类型为button的对象。如果没有返回类型按钮的对象,则在尝试添加处理程序之前,您必须添加一些其他类型检查和转换。
Public Function AddEventHandler(ByVal iColumnIndex As Integer, ByVal iRowIndex As Integer, ByVal eh As Action(Of EventHandler)) As Boolean
Dim bReturn As Boolean = False
Dim foundButton as Button
foundButton = m_TblLoPnl.GetControlFromPosition(iColumnIndex, iRowIndex)
AddHandler foundButton.Click, AddressOf myClickHandler
Return bReturn
End Function
Private Sub myClickHandler(sender as Object, e as EventArgs)
'Event Handler Code
End Sub