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

Getting More from the MessageBox Object

By now, you’ve learned nearly everything you need in order to write the Lunar Lander game. However, that program has one, seemingly little, feature that is very useful and bears explanation. When the game is over, the user gets a message box that asks whether the user wants to continue. Such a message box requires more than one button. All the other message boxes you have seen have only one button. The two−button version of the message box uses the same MessageBox class you’ve already used, but it works slightly differently.

In the Real World

It is very reasonable for you to wonder how you’re supposed to know that picBall has a Bounds property, that Bounds is a Rectangle, and so on. You aren’t supposed to know it! The .NET environment is far too comprehensive for any one programmer to understand fully. It’s more important that you know how to think about the environment and how to look up what you need than to have all the details socked away in your head.

I solved the collision detection problem by thinking through the STAIR process. I knew that I had two picture boxes, and I wanted to know whether there is a method that determines whether they intersect. I was surprised to find out that neither the PictureBox class nor any of its ancestors has any sort of collision method. I then went searching through the documentation for some type of object that does have the capability to check for intersections. I discovered that the Rectangle class has this capacity. After that, all I needed to do was figure out how to get a Rectangle related to a particular component, and I found the Bounds property. There are certainly other ways to solve this problem, but this one works. Programming is not about knowing how to do things. It’s about knowing how to figure out how to do things.

Introducing the MsgDemo Program

I created a small program to illustrate how you can squeeze much more information out of the MessageBox class. Figures 7.16 through 7.18 show the MsgDemo program.

Figure 7.16: Nothing happens until the user clicks the button.

190

Figure 7.17: Notice the question icon, the two buttons, and the labels on the buttons.

Figure 7.18: Apparently, there is a mechanism for reading which button was clicked.

Message boxes seem to be extremely simple but can be very tricky to program. Part of the problem is the need for consistency. It’s usually a good idea for your programs to use the same kinds of conventions your users have seen many times. Therefore, using the built−in dialogs is often better than building your own. Making basic modifications to the MessageBox class is easy but takes planning and digging around in documentation to find exactly what you want.

I wrote the MsgDemo specifically so that all the relevant code would go in the click event of the sole button. Here’s the code:

private void btnYesNo_Click(object sender, System.EventArgs e) { DialogResult reply;

reply = MessageBox.Show(

"Do you want fries with that?", "Yes or No Demo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (reply == DialogResult.Yes){ MessageBox.Show("Here are your fries");

}else {

MessageBox.Show("No fries");

191

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