In general, the functionality of a window is expressed by two types of items: controls and menus. It is through these items that a user interacts with your program. Menus are described later in this chapter. Here you will see how to add a control to a window.
Windows defines many different types of controls, including pushbuttons, check boxes, radio buttons, and list boxes, to name just a few. Although each type of control is different, they all work in more or less the same way. Here, we will add a pushbutton to a window, but the same basic procedure can be used to add other types of controls.
A pushbutton is encapsulated by the Button class. It inherits the abstract class ButtonBase, which inherits the Control class. Button defines only one constructor, which is shown here:
public Button( )
This creates a button that has a default size and location within the window. It contains no description. Before a button can be used, it will need to be given a description by assigning a string to its Text property.
To specify the location of the button within the window, you must assign the coordinates of its upper-left corner to the Location property. The Location property is inherited from Control and defined like this:
public Point Location { get; set; }
The coordinates are contained within a Point structure, which is defined in the System.Drawing namespace. It includes these two properties:
public int X { get; set; } public int Y { get; set; }
Thus, to create a button that contains the description “Press Here” and is positioned at location 100, 200, you use the following sequence:
Button MyButton = new Button(); MyButton.Text = "Press Here"; MyButton.Location = new Point(100, 200);
After you have created a button, you must add it to a form. You do this by calling the Add( ) method on the collection of controls linked to that form. This collection is available through the Controls property, which is inherited from the Control class. The Add( ) method is defined like this:
public virtual void Add(Control cntl)
Here, cntl is the control being added. Once a control has been added to a form, it will be displayed when the form is displayed.
The following program adds a button to the skeleton shown earlier. At this time, the button does not do anything, but it is present in the form and can be clicked.
// Add a Button. using System; using System.Windows.Forms; using System.Drawing; class ButtonForm : Form { Button MyButton = new Button(); public ButtonForm() { Text = "Using a Button"; MyButton = new Button(); MyButton.Text = "Press Here"; MyButton.Location = new Point(100, 200); Controls.Add(MyButton); } [STAThread] public static void Main() { ButtonForm skel = new ButtonForm(); Application.Run(skel); } }
This program creates a class called ButtonForm, which is derived from Form. It contains a Button field called MyButton. Inside the constructor, the button is created, initialized, and added to the form. When run, the program displays the window shown in Figure 26-2. You can click the button, but nothing will happen. To make the button do something, you must add a message handler, as described in the next section.
Figure 26-2: Adding a button