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

uses random numbers, I created an instance of the Random class. The toHit variable comes from the Random object’s nextDouble() method. Remember, the program will be comparing against a double value from the lookup table, so it needs to be a double as well. If toHit is less than chance (which was set when the user made a selection from one of the list boxes), the method reports a hit. Otherwise, the result label gets the value Miss.

Designing Programs by Hand

Arrays are powerful, as you see in the examples in this chapter. You can even make arrays out of complicated types such as classes and even screen components such as buttons and labels. Arrays of controls are especially useful, but sadly, the Visual Designer does not provide an easy way to work with them. The Soccer program will use such an array to hold the player images as they move around the field. Although the Visual Designer is very convenient, it can’t always create the exact type of program you need, so you have to know how to build a form without the designer.

Examining the Form by Hand Program

The Form by Hand program shown in Figure 8.12 looks much like any other Windows program. The interesting thing about this form is that it was built without the Visual Designer. Figure 8.13 shows the Visual Designer for the completed program.

Figure 8.12: The form has a button and responds to the button’s click event.

220

Figure 8.13: The Visual Designer shows no components at all!

It is possible (and sometimes desirable) to add controls to a program without using the Form Designer. Even when you do use the designer, you should know what it’s doing behind the scenes. I added the button and set up its event behavior entirely with code, without using the designer at all.

Adding Components in the Constructor

The constructor is a good place to add various components because it happens early. The Visual Designer places all its code in the InitializeComponent() method. To make sure that your code takes precedence over any automatically generated code, place your code in the constructor after the call to InitializeComponent():

public FrmByHand()

{

InitializeComponent();

Form1.Title = "Form by Hand"; Button myButton = new Button();

myButton.Location = new Point(50, 50); myButton.Size = new Size(100, 30); myButton.Text = "Click me!"; this.Controls.Add(myButton);

myButton.Click += new EventHandler(myButton_Click);

}// end constructor

The form itself is simply an object of type Form, so you can change its properties, just like any other object. The button is, likewise, simply a class. You can assign a point as the size property of the button (or any other component), and you set the size property by assigning a Size object. To determine the appropriate size and location, you can use graph paper, as well as experiment by trial and error. The program then sets the text to the button so that it has an appropriate caption. To add the control to the form, you add it to the form’s controls collection. This is much like adding an item

221

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