System.Object和System.EventArgs

时间:2018-08-16 22:47:32

标签: vba ms-access events access-vba addhandler

我正在尝试编写一个用于选择日期的简单日历。我有一个PushButtons数组,现在我正在尝试以编程方式添加事件处理程序。

Public Sub Initialize()

    If EventID = 0 Then
        GetEmployeeData
        EventType = "Attendance"
    Else
        GetEventData
    End If

    Dim Days
    Days = Array(Sunday0, Monday0, Tuesday0, Wednesday0, Thursday0, Friday0, Saturday0, _
    Sunday1, Monday1, Tuesday1, Wednesday1, Thursday1, Friday1, Saturday1, _
    Sunday2, Monday2, Tuesday2, Wednesday2, Thursday2, Friday2, Saturday2, _
    Sunday3, Monday3, Tuesday3, Wednesday3, Thursday3, Friday3, Saturday3, _
    Sunday4, Monday4, Tuesday4, Wednesday4, Thursday4, Friday4, Saturday4, _
    Sunday5, Monday5, Tuesday5, Wednesday5, Thursday5, Friday5, Saturday5)

    Dim j As Long
    For j = 0 To 41
        AddHandler Days(j).Click, AddressOf Calendar_Click
    Next j
End Sub

Public Sub Calendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim CalendarDay As Integer
    CalendarDay = Convert.Int32(sender.Caption)
    MsgBox (CalendarDay)
End Sub

但是,当我尝试运行该表单时,我看到一条错误消息

  

编译错误:未定义用户定义的类型。

更新

我将事件处理程序签名更改为此:

Public Sub Calendar_Click(ByVal sender As Object)

但现在出现以下错误:

  

编译错误:无效使用AddressOf运算符

2 个答案:

答案 0 :(得分:0)

您当然可以在Access中使用动态事件处理程序。但是每个对象都需要有自己的事件处理程序。

您可以为每个按钮使用一个单独的类,并使用一个类来管理按钮和事件。

您可以使用以下代码:

clsCalendar (创建事件处理程序并接收回事件的类)

Dim collButtonHandlers As Collection
Public Sub Initialize()
    Dim Days
    Days = Array(Sunday0, Monday0, Tuesday0, Wednesday0, Thursday0, Friday0, Saturday0, _
    Sunday1, Monday1, Tuesday1, Wednesday1, Thursday1, Friday1, Saturday1, _
    Sunday2, Monday2, Tuesday2, Wednesday2, Thursday2, Friday2, Saturday2, _
    Sunday3, Monday3, Tuesday3, Wednesday3, Thursday3, Friday3, Saturday3, _
    Sunday4, Monday4, Tuesday4, Wednesday4, Thursday4, Friday4, Saturday4, _
    Sunday5, Monday5, Tuesday5, Wednesday5, Thursday5, Friday5, Saturday5)
    'I assume that array is now filled with command buttons
    'Consider taking the array as input from a form
    Dim j As Long
    For j = 0 To 41
        AddButtonHandler Days(j)
    Next j
End Sub

Public Sub AddButtonHandler(btn As Access.CommandButton)
    Dim ButtonHandler As New clsCalendarButtonHandler
    ButtonHandler.Initialize btn, Me
    collButtonHandlers.Add ButtonHandler
End Sub

Public Sub Calendar_Click(btn As Access.CommandButton)
    Dim CalendarDay As Integer
    CalendarDay = CLng(btn.Caption)
    MsgBox (CalendarDay)
End Sub

clsCalendarButtonHandler (管理单个按钮事件并将其传递回管理类的类):

Dim cCalendar As clsCalendar
Dim btn As Access.CommandButton
Public Sub Initialize(cmdBtn As Access.CommandButton, Calendar As clsCalendar)
    Set cCalendar = Calendar
    Set btn = cmdBtn
    btn.OnClick = "[Event Procedure]"
End Sub

Private Sub btn_Click()
    cCalendar.Calendar_Click(btn)
End Sub

答案 1 :(得分:-1)

这在Access 2016中是不可能的。处理程序存在于VB.Net中,但在Access VBA中不存在。我只能想到这样的事情。但是如果有意义的话,还有另一个问题:

Public Sub MyEventSelection(Day As String)

  Select Case Day

    Case "Sunday0"
      Call Sunday0()
    Case "Monday0"
      Call Monday0()

    'And so on...

  End Select

End Sub

Public Sub Sunday0()
  'Do Stuff
End Sub

Public Sub Monday0()
  'Do Stuff
End Sub

也许您应该重新考虑所需的代码。