The code uses one of two methods for displaying the message box. When you
don’t define help, it passes just the prompt, the style options, and the title to
the
MsgBox
function. When you do define help, the code also passes the help
filename and the help context to the
MsgBox
function.
At this point, the
Show
method pauses. It waits for you to click one of the but-
tons on the message box. After you click one of the buttons, control returns
to the
Show
method.
The code calls the
RaiseEvent
method next. See the following “Defining
events” section for more information about events. In this case, the code
raises the
Click
event to show that you’ve clicked a button. Notice that the
Click
event receives the return value from the
MsgBox
function call. Finally,
the
Show
method also returns the result to the caller. Using this approach
lets you react to a message box return value as either a method call return or
an event, which increases the flexibility of the
MsgBox
function. You can also
choose to ignore the return value.
Defining events
Events are an essential part of most classes. They signify that you’ve done
something to the control. The example provides two kinds of events, but
the actual number that you can create is unlimited. Anytime that an action
occurs, you can
fire
(raise) an event. However, most classes limit their events
to a user action or a change in data.
The example code in the preceding “Defining methods” section shows one
way to fire an event. You use the
RaiseEvent
method to perform this task.
Before you can fire an event, you must define it. Events are always public,
but you should include the
Public
keyword to ensure that your event works
with future versions of VBA and that you avoid making the code hard to read.
Here are some examples of event declarations:
‘ Define an event that occurs when the user clicks a
button.
Public Event Click(Result As VbMsgBoxResult)
‘ Define events for various property changes.
Public Event ChangeButton(Result As ButtonTypes)
Public Event ChangeIcon(Result As IconTypes)
The event declarations always include the
Event
keyword and the event
name. The example uses
Click
as an event name because that’s what other
VBA objects use for this particular event. Arguments are optional. All three of
the declarations use single arguments in this case. I prefer to include informa-
tion that I think I might need later as part of the event declaration rather than
get that information by using other means.
193
Do'stlaringiz bilan baham: