Learn VB .NET Through Game Programming [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Learn VB .NET Through Game Programming [Electronic resources] - نسخه متنی

Matthew Tagliaferri

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید





Setting Up Events, or Making the Program Do Something


The program now has a form class named fGuess that contains seven labels, aproperty named Guess, and a routine named UpdateGuessButtons that colors the six numbered labels to match the value in the property. What the program doesn’t have yet is something that calls this routine. You want the UpdateGuessButtons routine to execute when a user clicks one of the six numbered labels. To accomplish the execution of code, the program needs an event handler.

Windows programs operate using an event-driven model, meaning that the program usually just sits around waiting for something (an event) to happen. This something is most commonly a mouse click or a key press. The type of mouse click (where clicked, which button, button going up or down) or the type of key press (which key, key going down or up) determines what the program does. Many of these events are preprogrammed—for instance, clicking in the title bar of a window and dragging it moves that window around on the desktop. You don’t have to program that functionality into VB .NET programs because the functionality is built into the operating system. Ditto for sizing a window by dragging the lower-right corner—that functionality is built into the operating system.

However, coloring the six buttons (actually, labels) when one of them is clicked on isn’t standard functionality, so you’ll have to code that functionality. This means you have to write an event handler for the Click event of the six label controls. Listing 1-4 shows the code for that event handler.

Listing 1.4: Click Event Handler for the Numbered Labels



Private Sub cbButtonClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles lbOne.Click, _
lbTwo.Click, lbThree.Click, lbFour.Click, lbFive.Click, lbSix.Click
Guess = CInt(CType(sender, Label).Text)
Call UpdateGuessButtons()
Call RollTheDie()
If dieResult = Guess Then
lbResult.Text = "Correct!"
Else
lbResult.Text = "Try Again"
End If
End Sub



An event handler in VB is just a standard procedure (declared Sub). They’re also declared private, meaning that they’re not callable from the outside of this class. This is desirable because event handler routines are usually not called directly in a program anyway—they’re called as a result of the desired events occurring.

In this case, the desired event is one of the six labels being clicked. You can see that the declaration of this event handler states that this event is “attached” to the events lbOne.Click, lbTwo.Click, and so on all the way to lbSix.Click using the Handles keyword.

To set up an event handler, you use the code editor. At the top of the code-editing window are two drop-down boxes. The left drop-down box lists all controls on the current form. Once you select a control, the right drop-down list shows all the event handlers available for this control (see Figure 1-10). Selecting one of these event handlers adds an empty event handler procedure to your code.


Figure 1-10: Viewing available event handlers for a control using the two drop-down boxes in the code editor

As you can see in Listing 1-4, you can make the same event handler handle events on multiple controls by listing those events after the Handles clause. This is easy to do manually.

The event handler in Listing 1-4 does a few things. The first thing it does is set the Guess property discussed in the previous section to some value. It obtains that value by casting the variable sender (which is passed into the event handler and represents the label that the user clicked) to a label type, then obtaining the value of the Text property of that label, and in turn converting that text (string) value to an integer. All of those things happen in a single line of code:


Guess = CInt(CType(sender, Label).Text)

Stated another way, when the user clicks the 3 label, this code converts the passed-in sender parameter to a label, converts the text of that label to an integer value, and stores it in the Guess property.




Code Size vs. Complexity


The previous lone line of code functions perfectly, but it does quite a few things at one time, which could be confusing to another programmer trying to figure out what the code is doing. There’s a constant battle between keeping a program “short and sweet” and keeping it easy to read. You could rewrite the single line of code in the following, more verbose way:


Dim lb as Label
lb = CType(sender, Label)
Guess = Cint(lb.Text)

As always, it’s up to the developer to decide whether to go for compactness or readability. Factors helping to determine this decision include whether other developers might someday be responsible for your code, your company, or your department coding standards and how you feel that day.



After the Guess integer is populated based on the clicked label, the next line of code calls the UpdateGuessButtons procedure (which has already been discussed). This procedure, as you might recall, colors the six labels in such a way that the one matching the value of the Guess property turns blue. The complete effect is the user clicking one of the labels and having that label change color.

The following line calls a procedure named RollTheDie. You’ll learn more about this procedure in the “Tying the Functionality Together” section.

Finally, a new variable named dieResult, is compared with the value of the Guess property. If the values are equal, the seventh label changes to read Correct!, meaning the user correctly guessed the value of the die roll. If the value of the two variables isn’t equal, the message Try Again displays in the seventh label.

This simple event handler represents the complete “flow” of the Guess the Die Roll game. When one of the numbered labels is clicked, the value is stored, the correct label is colored, the die is rolled, and the user is notified whether she won or lost. The game then waits for the user to click one of the labels again, and the process repeats.

/ 106