Section 18.2. Events

   

18.2 Events

When something happens in a program, we call that occurrence an event . Clicking on a button is an event. Closing a window, dropping down a listbox, pressing a key, and moving the mouse are all events. Windows is an event-driven operating system.

How does the operating system know when you've pressed a key? There are, in general, two ways for the system to receive notification. One way is called polling . In this approach, the operating system asks the keyboard: "Hey! Do you have a keypress for me? Nope? Okay, I'll check again in few milliseconds." This is very much the same as the way I find out if I have mail. Every few milliseconds , I walk down to the mailbox and look inside. Sometimes there is mail; most of the time it is empty.

The second, much preferable, way to keep the operating system apprised of events is to ask the keyboard to notify the operating system when a key is pressed. This is analogous to how the telephone works. You don't pick up the phone every few minutes to see if you have a call. Instead, you leave the phone alone, and when there is a call, the phone notifies you (typically, just as you sit down to dinner).

This act of notifying you is called raising an event . In this case, the event is "a call is ready for you."

VB.NET provides extensive support for handling events such as button clicks. The Button object is declared with the keyword WithEvents . Right-click on your form and choose the View Code item from the pop-up menu. VS.NET will display the code that creates your form, as shown in Figure 18-7.

Figure 18-7. Code view
figs/lvbn_1807.gif

It looks like not much is there. Notice, however, that in the middle of the code a gray box contains the words "Windows Form Designer generated code." To the left of the box is a plus sign. Click on the plus sign to expand this region of code that was created by VS.NET.

Inside this area, shown in Figure 18-8, VS.NET has provided your class with a constructor, a Dispose( ) method, and various declarations. Just below the Dispose( ) method are the declarations of the two controls you've added to your form, the Label and the Button, as shown circled and highlighted in Figure 18-8.

Figure 18-8. The Label and the Button declared
figs/lvbn_1808.gif

Notice that the declaration of both controls includes the keyword WithEvents . This keyword indicates that these controls will raise events. The Button class raises a number of events, as you can discover by looking up the Button class in the documentation, as shown in Figure 18-9.

Figure 18-9. Button documentation
figs/lvbn_1809.gif

Surprisingly, the label control also raises a great many events, though it is uncommon to respond to clicks and mouse movements over a label.

The event we care about is the Button's Click event, which is raised every time the Button is clicked.

Each control has a default event, and the Button's default event is Click. You can create the event handler for the default event by going back to the Design view and then double-clicking on the Button.

Doing so causes VS.NET to create a skeleton event handler for you, in this case a method called btnChange_Click( ):

 Private Sub btnChange_Click( _          ByVal sender As System.Object, _          ByVal e As System.EventArgs) _            Handles btnChange.Click     End Sub 

Every event handler takes two parameters. The first is of type Object and is called sender, by convention. This is the control that raised the event. The second is of type EventArgs (or a class derived from EventArgs) and is a structure that contains information about the event. Often this structure has no useful content, but for some events, this structure provides useful information for handling the event. (Structures are discussed in depth in Chapter 12.)

The method declaration includes the keyword Handles followed by the event that the method is designed to handle. In this case, you've declared that the btnChange_Click( ) method will handle the Click event for the control btnChange.

All you have to do is write the code within the method for whatever is supposed to happen when the button is clicked. In this case, you'd like to change the contents of the label when the button is clicked. Add the following code to the event handler method:

 Private Sub btnChange_Click( _    ByVal sender As System.Object, _    ByVal e As System.EventArgs) _    Handles btnChange.Click  lblOutput.Text = "Goodbye!"   lblOutput.BackColor = Color.Blue   lblOutput.ForeColor = Color.Yellow  End Sub 

This code will cause the text of the label to change, along with its background color and foreground color. Run the application with Control-F5. Click on the button. Hey! Presto! The text changes, as shown in Figure 18-10.

Figure 18-10. Testing the event handler
figs/lvbn_1810.gif

To ensure that you fully understand what is happening with this code, put a breakpoint in the event handler and then run the program in debug mode. When you click on the button, you'll see the program stop at the breakpoint. Creating complex Windows applications now becomes largely a matter of dragging the controls onto the form and wiring up the event handlers to do the work you want when the various events are fired .

18.2.1 Web Applications

One of the most powerful aspects of VS.NET and the .NET platform is that the tools for Rapid Application Development that are provided for creating Windows applications are also available for building Web applications. To see how similar web development is to Windows development, close the current project by choosing the Close Solution item from the File menu. Then choose New Project from the Start menu. This time, rather than choosing a Windows Application as the Project Type, choose an ASP.NET Web Application; then enter a pathname for the Hello World program in the Location box, as shown in Figure 18-11.

Figure 18-11. Creating Hello World as an ASP.NET application
figs/lvbn_1811.gif

VS.NET will create a virtual directory for you on your web server (for this example, you'll need to have IIS installed), and will open a development environment not very different from that available for creating Windows applications.

Once again you'll find a Toolbox along the lefthand side of the VS.NET window, and a Properties window docked to the righthand side. ASP.NET applications work by creating an HTML file named with an .aspx extension. When you right-click on the form and select View Code, VS.NET also creates an associated code-behind file. (Code-behind files are integral to most serious applications but are beyond the scope of this primer. See Chapter 19 for pointers to a number of useful books on advanced VB.NET programming and ASP.NET development.) Your form's default name is WebForm1.aspx, and the associated code-behind file will be WebForm1.aspx.vb .

From the Toolbox, drag a label onto the form. As you did in the section on Windows applications, set the label's name to lblOutput and its text to "Hello World!" Then click on the Font property and set Bold to true and set the size to x-large. Remember, this program will run in a browser, so the properties are restricted to those supported by HTML and browsers.

Now go back to the Toolbox and drag a button onto the form. Set the button's name to btnChange and its text to "Change!". Set its BackColor property to red and its ForeColor to yellow. Then set the button's font to bold. The results are shown in Figure 18-12.

Figure 18-12. Creating the web form
figs/lvbn_1812.gif

If you double-click on the button, VS.NET creates the code-behind file and takes you to the event handler that it has created for you:

 Private Sub btnChange_Click( _          ByVal sender As System.Object, _          ByVal e As System.EventArgs) _            Handles btnChange.Click     End Sub 

Your job is to fill in the code that will be run when the event is fired. You might begin by copying in the code from the event handler you created for the Windows application, earlier in this chapter:

 Private Sub btnChange_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnChange.Click    lblOutput.Text = "Goodbye!"   lblOutput.BackColor = Color.Blue   lblOutput.ForeColor = Color.Yellow  End Sub 

Now run the application. This time a browser opens to test your application, within which the text and button appear. Click on the button. Hey! Presto! the page is redrawn with the new text and coloring, as shown in Figure 18-13.

Figure 18-13. Handling the event in a browser
figs/lvbn_1813.gif
   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net