The following table describes some of the most useful Form events.
Event | Description |
---|---|
Activated | Occurs when the form activates. |
Click | Occurs when the user clicks on the form. Normally, if the user clicks a con trol, the control rather than the form receives the Click event. If the form’s Capture property is set to True, however, the event goes to the form. |
ControlAdded | Occurs when a new control is added to the form. |
ControlRemoved | Occurs when a control is removed from the form. |
Deactivate | Occurs when the form deactivates. |
DoubleClick | Occurs when the user double-clicks the form. Normally, if the user double-clicks a control, the control rather than the form receives the DoubleClick event. If the form’s Capture property is set to True, however, the first click goes to the form and the second goes to the control. |
DragDrop | Occurs when the user drops data onto the form. The form should process the data in an appropriate way. See Chapter 13 for more information on drag-and-drop operations. |
DragEnter | Occurs when a drag-and-drop operation moves over the form. The form should indicate what drag operations it will allow and optionally display a visible indication that the drag is over it. Refer to Chapter 13 for more information on drag-and-drop operations. |
DragLeave | Occurs when a drag-and-drop operation leaves the form. If the form is displaying a visible indicator of the pending drop, it should remove that indicator now. See Chapter 13 for more information on drag-and-drop operations. |
DragOver | Occurs repeatedly as long as a drag-and-drop operation is being performed over the form. The form can use this event to display a more complex visible indicator of the pending drop. For example, it might show where on the form the data will be dropped or it might highlight the area on the form under the mouse. See Chapter 13 for more information on drag-and-drop operations. |
FormClosed | Occurs when the form is closed. The program can still access the form’s properties, methods, and controls, but it is going away. See also the FormClosing event. Note that if the program calls Application.Exit, then the form’s FormClosed and FormClosing events do not occur. If you want the program to free resources before the form disappears, it should do so before calling Application.Exit. |
FormClosing | Occurs when the form is about to close. The program can cancel the close (for example, if some data has not been saved) by setting the even handler’s e.Cancel parameter to True. |
GiveFeedback | Occurs when a drag moves over a valid drop target. The source can take action to indicate the type of drop allowed. For example, it might change the drag cursor displayed. See Chapter 13 for more information on drag-and-drop operations. |
GotFocus | Occurs when focus moves into the form. |
HelpRequested | Occurs when the user requests help from the form, usually by pressing F1 or by pressing a context-sensitive Help button (see the HelpButton property) and then clicking a control on the form. Help requests move up through control containers until a HelpRequested event sets its hlpevent.Handled parameter to True. For example, suppose that the user sets focus to a TextBox contained in the form and presses F1. The TextBox control’s HelpRequested event handler executes. If that routine doesn’t set hlpevent.Handled to True, the event bubbles up to the TextBox control’s container, the form, and its HelpRequested event handler executes. |
KeyDown | Occurs when the user presses a keyboard key down. |
KeyPress | Occurs when the user presses and releases a keyboard key. |
KeyUp | Occurs when the user releases a keyboard key. |
Layout | Occurs when the form should reposition its child controls. If your code needs to perform custom repositioning, this is the event where it should do so. |
Load | Occurs after the form is loaded but before it is displayed. You can perform one-time initialization tasks here. |
LostFocus | Occurs when the focus moves out of the form. |
MdiChildActivate | Occurs when an MDI child form contained in this MDI parent form is activated or closed. This activation only applies to the MDI children within this form. For example, setting focus to a different form or application and then back to the MDI child does not raise this event, but switching back and forth between two MDI children does. This event basically occurs when the MDI parent’s active MDI child changes. You can catch the event to update the MDI parent’s menus or perform other actions when the active child changes. |
MouseClick | Occurs when the user clicks the form. You should consider the Click event to be on a logically higher level than MouseClick. For example, the Click event may be triggered by actions other than an actual mouse click (such as the user pressing the Enter key). |
MouseDoubleClick | Occurs when the user double-clicks the form. You should consider the DoubleClick event to be on a logically higher level than MouseDoubleClick. |
MouseDown | Occurs when the user presses the mouse down over the form. Also see the Capture property. |
MouseEnter | Occurs when the mouse first moves so that it is over the form. If the mouse moves over one of the form’s controls, that counts as leaving the form, so when it moves back over an unoccupied part of the form, it raises a MouseEnter event. |
MouseHover | Occurs when the mouse remains stationary over the form for a while. This event is raised once when the mouse first hovers and then is not raised again until the mouse leaves the form and returns. Note that the mouse moving over one of the form’s controls counts as leaving. |
MouseLeave | Occurs when the mouse leaves the form. Note that the mouse moving over one of the form’s controls counts as leaving. |
MouseMove | Occurs when the mouse moves while over the form. |
MouseUp | Occurs when the user releases the mouse button. When the user presses a mouse button down, the form will capture subsequent mouse events until the user releases the button. While the capture is in place, the form receives MouseMove events, even if the mouse is moving off of the form. It will receive a MouseHover event, even if the mouse is off of the form, if no such event has been raised since the last time the mouse moved over the form. When the user finally releases the button, the form receives a MouseUp event and then, if the mouse is no longer over the form, a MouseLeave event. |
MouseWheel | Occurs when the user moves the mouse wheel. The event’s e.X and e.Y parameters give the mouse’s current position. The e.Delta parameter gives the signed distance by which the wheel has been rotated. Currently, this is defined as 120 detents per notch of the wheel. Standards dictate that you should scroll data when the accumulated delta reaches plus or minus 120 detents, and that you should then scroll the data by the number of lines given by SystemInformation .MouseWheelScrollLines (currently this is 3). If higher-resolution mouse wheels are added some day, a notch might send a value smaller than 120, and you could update the data more often, but you should keep the same ratio: SystemInformation.MouseWheelScrollLines lines per 120 detents. |
Move | Occurs when the form is moved. |
Paint | Occurs when part of the form must be redrawn. You can use the e.ClipRectangle parameter to see what area needs to be drawn. For very complicated drawings, you may be able to draw more quickly if you only draw the area indicated by e.ClipRectangle. Note also that Visual Basic clips drawings outside of this rectangle and may clip some areas inside this rectangle that do not need to be redrawn. That makes drawing faster in some cases. The idea here is that part of the form has been covered and exposed so only that part must be redrawn. If you need to adjust the drawing when the form is resized, you should invalidate the form in the Resize event handler to force a redraw of the whole form. |
QueryContinueDrag | Occurs during a drag-and-drop operation (with this form as the drag source) when the keyboard or mouse button state has changed. The form can decide to continue the drag, cancel the drag, or drop the data immediately. See Chapter 13 for more information on drag-and-drop operations. |
Resize | Occurs when the form is resized. |
ResizeBegin | Occurs when the user starts resizing the form. |
ResizeEnd | Occurs when the user has finished resizing the form. |
SizeChanged | Occurs when the form is resized. |
When focus moves into and out of a form, the sequence of events is: Activated, GotFocus, Deactivate, Validating, Validated, LostFocus.
Typically, when the user clicks on the form, the sequence of events is: MouseDown, Click, MouseClick, MouseUp.
Typically, when the user double-clicks the form, the sequence of events is: MouseDown, Click, MouseClick, MouseUp, MouseDown, DoubleClick, MouseDoubleClick, MouseUp.
When code resizes the form, the sequence of events is: Resize, SizeChanged.
When the user resizes the form, the sequence of events is: ResizeBegin, Resize, SizeChanged, Resize, SizeChanged, . . ., ResizeEnd.