Forms react to events. This is known as event handling . An event is generated due to many different actions, such as when you resize a form, click the form, or move the mouse over the form, as well as when a change in time occurs or another application performs some type of action that causes the current application to be affected. There are two basic methods of event handling:
Chapter 2, "Controls on Forms," discusses the details of triggering events. For our purposes here, we'll examine one of the more common triggering events used in graphical user interface (GUI) designthe MouseDown event, which is triggered when the user clicks a mouse button. Using the Handles KeywordThe easiest way to handle events is to supply an event-handling method and to use the Handles keyword to specify the event that it handles. For example, this method handles the MouseDown event of the form where the code is located (the MyBase keyword refers to the class on which the form is based): Private Sub Form1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown MessageBox.Show(String.Format("X ={0}, Y={1}", e.X, e.Y), _ String.Format("The {0} mouse button hit me at:", e.Button.ToString())) End Sub In this example, the MouseDown event triggers the display of the current position of the mouse over the form in a message box, as shown in Figure 1.5. Figure 1.5. A message box displaying the results of the MouseDown event.
The MouseEventArgs TypeThe MouseDown event example utilizes a derived class, MouseEventArgs , that contains event- related data. This class is derived from the System.Windows.EventArgs type. Table 1.4 lists some of the properties of the MouseEventArgs type. Table 1.4. The MouseEventArgs Properties
The MessageBox ClassThe MouseDown example also uses a MessageBox object to display information to the user. This is a useful class to call when performing debugging because you can display a message box showing the data values and details of processing as the code is run. The MessageBox class is located in the Systems.Windows.Forms namespace and inherits from the Object class. It also includes several Show() options that are unique to its class. Within these are several enumerated arguments, including the values for MessageBoxButtons and MessageBoxIcons .
When manipulating instances of the MessageBox class, you should know the enumerated members available in the MessageBoxButtons arguments (see Table 1.5) and the MessageBoxIcons arguments (see Table 1.6). Table 1.5. The MessageBoxButtons Enumerator List
Table 1.6. The MessageBoxIcons Enumerator List
Attaching a DelegateAnother option for handling an event is to create your own event-handling code and attach the custom function using the AddHandler method. In order to add the same event handling as the previous example, using a delegated function, the following would be added to the form's code: Sub Form1_Mousedown(ByVal sender As Object, _ ByVal e As MouseEventArgs) MessageBox.Show(String.Format("X ={0}, Y={1}", e.X, e.Y), _ String.Format("The {0} mouse button hit me at:", e.Button.ToString())) End Sub In order to delegate the MouseClick subroutine to perform event handling, you would also need to add this line to the form's constructor: AddHandler Me.MouseDown, AddressOf Me.Form1_MouseDown The AddHandler method specifies the delegation of the Me.Form1_MouseDown function to handle MouseDown events on the form. To detach the delegate, you would use the RemoveHandler method, which is similar in syntax to AddHandler . Attaching and detaching a delegate allows you to turn on and off a particular type of event-handling function or to change the event-handling delegate during different parts of your application's operation.
|