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.


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 to a list box, but the controls collection is designed to accept controls. The final task for the constructor is to register an action listener to the button. The last line of the method accomplishes this task. myButton.Click refers to the click event of the myButton object. You can use the += operator to add an event handler to this event.

You can add multiple handlers to the event, if you like, so that clicking the button calls several methods. Doing so can cause problems, though, because it is unclear which order the methods would evaluate.

An event handler is a class. To make one, you call its constructor with the name of the method you will use to respond to this event. That event method (like all event methods) will need to have an object and an EventArgs parameter.

Responding to the Button Event

Because I used myButton_Click as a parameter when I built my EventHandler, I have to create a method named myButton_Click:

public void myButton_Click(object sender, System.EventArgs e){
  MessageBox.Show("Ouch!");
} // end myButton

The only way this method differs from any other event handler is that the Visual Designer did not create it. I had to write the code by hand, including the parameters. Other than that, the method is no different from many you have written by now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.191.134