|
An event is something happening at a point in time. Visual Basic creates events to allow a time-static spreadsheet to handle real-world situations at the time they occur. Events are of two types: those created by code working with objects, and those caused directly by user actions.
The UserForm itself only has three unique events, which often aren’t used. In addition, there are events common to many objects. UserForm events available:
UserForm_Activate (unique to UserForm)
UserForm_AddControl
UserForm_BeforeDragOver
UserForm_BeforeDropOrPaste
UserForm_Click
UserForm_DblClick
UserForm_Deactivate (unique to UserForm)
UserForm_Initialize
UserForm_KeyDown
UserForm_KeyPress
UserForm_Layout
UserForm_MouseDown
UserForm_MouseMove
UserForm_MouseUp
UserForm_QueryClose
UserForm_RemoveControl
UserForm_Resize
UserForm_Scroll
UserForm_Terminate (unique to UserForm)
UserForm_Zoom
As can be seen from the above list, very sophisticated applications can be written. UserForm_Initialize is given the most attention in these lessons. The other events will more likely be used for specific controls and not for the UserForm. For example, there might be a use for double-clicking a ListBox, or even a Label, but it’s less like to have a use for double-clicking the UserForm itself.
Many events take arguments. The best way to ensure the event handler has the correct format is to select the event from the Procedure drop-down above the code pane. The handler will be created correctly, and the programmer can proceed from that point.
EXAMPLE
For UserForm_QueryClose(Cancel As Integer, CloseMode As Integer), the Cancel argument defaults to False. Setting it to True prevents the UserForm from closing. The system sets CloseMode according to what is attempting to close the UserForm, and CloseMode = 0 (vbFormControlMenu) is set when the user attempts to directly close the UserForm, such as by clicking the X in the top right corner of the UserForm.
Private
Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then Cancel = True ' Cancel if user closes
End Sub
This code would prevent the user from exiting the UserForm via the X. Before implementing it, be sure to first create a Close button, and code the CloseButton_Click event to unload the UserForm. If this is not done, the user will have no way to nicely close the UserForm.
To see the events available to UserForm or any of the controls, go to the UserForm object view and double-click the item of interest. This will create a Click event handler in the Code view. The complete list of Events can then be seen by clicking the drop-down arrow on the Procedure drop-down box on the upper right side, above the code pane. This list of available events will be different for many of the controls. A label, for example, will have many fewer events than a ComboBox.
UserForm Order of Events
The UserForm_Initialize event occurs first, after a Load statement. If the Load occurs because a Show was used without a prior Load, then the Initialize event occurs after the UserForm is created, but before it is made visible.
The UserForm_Activate event occurs when the UserForm receives the focus, which is the same as becoming the active object. This can occur as the second part of a show, when the UserForm was previously hidden, or when the worksheet or another UserForm has been the active object.
The UserForm_Deactivate event occurs when the UserForm loses the focus.
The UserForm_Terminate event occurs after the UserForm is unloaded. If there are multiple instances of the UserForm, Terminate occurs only after the last instance unloads. Terminate may not occur if the UserForm closes due to an error.
|
OzGrid.com accepts no responsibility for any adverse affect that may result from undertaking our training.