Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

lstTarget.SelectedIndex = 1;

The SelectedIndex property is used to determine which element in a list box is currently selected. In this case, I chose to preset lstKicker at the 0 element (goalie) and lstTarget at element 1 (fullback).

Using a Custom Event Handler

After I designed the visual interface of the Shot Demo program, I started to think about the event handling. I soon realized that the exact same code should happen whenever either list box is changed, because the program will always need the values of both list boxes to determine how likely the shot will be. There are a couple ways to handle this. The most straightforward is to write code in the event handler for one list box and then copy and paste the code to the other. However, if you have to change the code, you need to change it in two places. Another solution would be to build a special method that evaluates the list boxes and to call that method from both event handlers. C# has another nice trick, though. Rather than have C# make event handlers automatically, you can build your own method and designate it to be an event handler. All event handlers must have two parameters. To be consistent with the automatically generated code, I used the same names as the default names for the sender and EventArgs variables.

The declaration for the new changeStatus method looks like this:

public void changeStatus(object sender, System.EventArgs e){

After you write a method that you want to use as an event handler, you connect it to the events it should respond to. This is easily done with the Visual Designer in the events panel of the Properties window. Figure 8.11 shows assigning a custom method to a list box’s events.

Figure 8.11: The drop−down menu is automatically populated with all methods detected by the IDE. The Visual Designer can detect event handlers by their parameters, so it automatically puts them in the list box associated with an event. In this way, you can assign several events to the same method, if you like.

218

Writing the changeStatus() Method

The behavior of the changeStatus() method is not difficult. It starts with two int variables representing the kicker and the target. The kicker variable contains the selectedIndex property of the lstKicker list box. Likewise, the target variable is extracted from the lstTarget list box. The selectedIndex property returns an integer showing which element in a list box is currently selected.

public void changeStatus(object sender, System.EventArgs e){

//handles a change in either listbox int kicker = 0;

int target = 0;

//get the kicker

kicker = lstKicker.SelectedIndex;

//get the target

target = lstTarget.SelectedIndex + 1;

chance = shotChance[kicker, target]; lblChance.Text = chance.ToString();

}// end changeStatus

When there is a legitimate value in the kicker and target variables, the method looks up a value in the shotChance array and assigns this value to the chance double. It also sends a text version of this value out to the text box to tell the user which value was extracted.

Kicking the Ball

The last part of the Shot Demo program involves calculating whether a shot is successful. If the Shot Demo program can demonstrate this behavior in this simpler environment, transferring this code to the more complex Soccer program should be easy:

private void btnKick_Click(object sender, System.EventArgs e) {

Random roller = new Random(); double toHit = roller.NextDouble(); if (toHit < chance){

lblResult.Text = "Hit";

}else {

lblResult.Text = "Miss";

}// end if

}// end btnKick

In the Real World

Two−dimensional arrays are often called lookup tables because they can be used just as I have done in this program. I stored information in a table and then looked it up by the row and column. You can use two−dimensional arrays any time what you are working on requires you to look up an item in a table. In nongame programming, lookup tables are frequently used for tasks such as determining shipping rates, sales tax, or any other value that might commonly be stored in a table.

Whenever the user clicks the button, this method springs into action. Like any other method that

219

Соседние файлы в предмете Программирование на C++