Whenever the user takes any action, such as clicking the mouse, pressing a key, passing out on the keyboard, or putting a bullet through the monitor, the action is called an event. The moment an event occurs, Visual Basic .NET looks for BASIC code to tell the program what to do. The BASIC code that responds to a specific event is called an event-handling procedure or just as event handler.
A single Visual Basic .NET program can consist of several thousand event-handling procedures. If you have that many, however, you have a tremendously complicated program or you're an incredibly incompetent programmer.
With so many possible events and so many possible event-handling procedures in a single program, how does Visual Basic .NET know which event-handling procedure to use?
The answer is easy. When an event occurs, this event is usually directed at some part of your program's user interface. For example, most users click the mouse button only when the mouse is pointing at an object, such as a button, check box, or menu command on the screen.
Every object can have one or more event-handling procedures, and each event-handling procedure responds to one specific event, such as clicking the mouse or pressing a key.
Naming the types of events
Events can be classified into three categories:
- Keyboard events: Occur when the user presses a certain key, such as Tab, or a certain keystroke combination, such as Ctrl+P.
- Mouse events: Occur when the user moves the mouse, clicks or double-clicks on the mouse button, or drags the mouse across the screen.
- Program events: Occur when a Visual Basic .NET program does something on its own, such as loading a form or changing the contents of a text box. Whereas keyboard and mouse events occur when the user does something, program events occur when BASIC code does something.
Although Visual Basic .NET can respond to a multitude of events, you generally want your user interface to respond only to a few events, such as the clicking of the mouse or the pressing of a certain key. As soon as Visual Basic .NET detects an event, your program immediately looks to see what part of the user interface needs to respond.
When the user clicks the mouse, for example, Visual Basic .NET first identifies the event. ("Okay, that was a mouse click.") Next, it looks to see where the user clicked the mouse. ("The user clicked the mouse on the OK button.")
Visual Basic .NET then finds that particular button's event-handling procedure, which contains BASIC code that tells your program what to do when the user clicks that particular button.
Creating event-handling procedures
One object can respond to one or more events. For example, a button can respond to the user clicking on the mouse button or pressing the Enter key.
Two or more objects can respond to the same event. For example, both a button and a check box can respond to a mouse click, but they (usually) have completely different instructions that tell Visual Basic .NET what to do next.
To write an event procedure, you have to perform the following tasks:
1. Identify the part of your user interface that is going to respond.
2. Open the code editor.
3. Identify the event to which Visual Basic .NET is to respond.
4. Write BASIC code to process the event.
Make sure that all the objects of your user interface have names before creating any event procedures. If you create an event-handling procedure for an object and later change that object's name, you will have to rewrite your event-handling procedures.
The following three parts of a user interface can have events associated with them:
- Forms
- Objects (buttons, check boxes, list boxes, and so on)
- Pull-down menus
To create an event-handling procedure for an object such as a form, button, pull-down menu, check box, and so on, follow these steps:
1. Open the form that contains the object that you want to create an event-handling procedure for, such as a button or a check box.
If you double-click on an object (such as a button or even the form itself), Visual Basic .NET displays the code editor right away and creates the most common event-handling procedure for your chosen object.
2. Open the code editor by pressing F7 or choosing View --> Code.
Visual Basic .NET displays the code editor on the screen.
3. Click in the Class Name list box.
A list of different objects appears, such as Label1 or Button2.
You should change the Name property of an object before creating an event-handling procedure for it. That way, the name can be descriptive of the object's purpose in your program rather than a generic name.
4. Click in the Method Name list box.
A list of different events for your chosen object appears, such as Click or MouseHover.
5. Click on the event you want your object to respond to (such as Click).
Visual Basic .NET creates an empty event-handling procedure. At this point, you can start writing BASIC code to make your event-handling procedure actually do something, such as change the properties of another object on your form.
To create an event-handling procedure for a pull-down menu command, follow these steps:
1. Click on the pull-down menu title containing the menu command you want.
2. Double-click on the menu command for which you want to create an event-handling procedure.
Visual Basic .NET creates an empty event-handling procedure.
dummies
Source:http://www.dummies.com/how-to/content/creating-eventhandling-procedures-with-visual-basi.html
No comments:
Post a Comment