问题
I'm trying to write a simple calendar for selecting dates. I have an array of PushButtons, and now I'm trying to programmatically add event handlers.
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
However, when I try to run the form, I get an error message reading
Compile Error: User-defined type not defined.
Update
I changed the event handler signature to this:
Public Sub Calendar_Click(ByVal sender As Object)
but now I get the following error:
Compile Error: Invalid use of AddressOf operator
回答1:
You can certainly use dynamic event handlers in Access. But every object needs to have it's own event handler.
You can use a separate class for each button, and a class to manage the buttons and the events.
You can use the following code:
clsCalendar (the class that creates event handlers, and receives the events back)
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 (the class that manages a single button event, and passes it back to the managing class):
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
回答2:
This isnt possible in Access 2016. Handlers exist in VB.Net but not in Access VBA. I can only think of something like that. But if its makes sense is another question:
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
Maybe you should rethink your needed code.
来源:https://stackoverflow.com/questions/51886276/system-object-and-system-eventargs