Previous Page
Next Page

Creating Your Application

As an example, you are going to create an application that allows a user to input and display details for members of the Middleshire Bell Ringers Association, an esteemed collection of the finest campanologists. Initially you will keep the application very simple, concentrating on laying out the form and making sure that it all works. In later chapters, you will provide menus and learn how to implement validation to ensure that the data that is entered makes sense. The following graphic shows what the application will look like after you have completed it. (You can see the completed version by running BellRingers.exe, located in the \Microsoft Press\Visual CSharp Step by Step\Chapter 20\BellRingers Complete\BellRingers\bin\Debug folder in your My Documents folder.)

Creating a Windows Forms Application

In this exercise, you'll start building the Middleshire Bell Ringers Association application by creating a new project, laying out the form, and adding Windows Forms controls to the form. Because you have been using existing Windows Forms applications in Microsoft Visual Studio 2005 in previous chapters, much of the first couple of exercises will be a review for you.

Create the Middleshire Bell Ringers Association project
  1. Start Visual Studio 2005.

  2. On the File menu, point to New, and then click Project.

  3. In the Project Types pane, select Visual C#.

  4. In the Templates pane, select Windows Application.

  5. In the Name text box, type BellRingers.

  6. In the Location list box, navigate to the Microsoft Press\Visual CSharp Step by Step\Chapter 20 folder in your My Documents folder.

  7. Click OK.

    The new project is created and contains a blank form called Form1.

Set the properties of the form
  1. Select the form in the Designer View window. In the Properties window, click the (Name) property, and then type MemberForm in the (Name) text box to change the name of the form. (If the Properties window is not displayed, click Properties Window on the View menu, or press F4.)

  2. In the Properties window, click the Text property, and then type Middleshire Bell Ringers Association – Members, to change the text in the title bar of the form.

  3. In the Properties window, click the BackgroundImage property, and then click the Ellipses button in the adjacent text box.

    The Select Resource dialog box opens.

  4. In the Select Resource dialog box, click Local resource and then click Import.

    The Open dialog box opens.

  5. In the Open dialog box, navigate to the \Microsoft Press\Visual CSharp Step by Step\Chapter 20 folder in your My Documents folder, select the Bell.gif file, and then click Open.

    Part of the image will be displayed in the Select Resource dialog box.

  6. In the Select Resource dialog box, click OK.

    The BackgroundImage property is now set to the bell image.

  7. In the Properties window, click the BackColor property, and then click the down-arrow button in the adjacent text box.

    A dialog box opens.

  8. On the System tab of the dialog box, click Window. This value sets the background color of all the controls that you drop onto the form to the same color as the window itself.

  9. Select the Font property. This is a composite property that has many attributes. In the Properties window, click the plus sign (+) to expand the Font property and display the attributes. Type 12 for the Size attribute of the font, and set the Bold attribute to True.

    You can also change some composite properties, such as Font, by clicking the ellipses button that appears when you select the property. When you click the ellipses button in the Font property, the standard Font dialog box opens and allows you to select the font and effects that you want.
  10. Change the form's Size property, which is also a composite property. In the Properties window, click the plus sign (+) to expand the Size property and display the attributes. Set the Width attribute to 600 and the Height attribute to 470.

    The form should look like the image in the following graphic.

  11. On the Build menu, click Build Solution.

    The form should build successfully.

  12. On the Debug menu, click Start Without Debugging.

    The application will start running and will display the main form containing the image. The form does not do anything useful yet, so close it and return to Visual Studio.

What Are the Common Windows Forms Properties?

If you look closely at the Properties window when a form is selected, you can see that there are over fifty properties available. Some of them are fairly obvious; for example, the Text property that corresponds to the text displayed in the form's title bar. Some properties are useful under certain circumstances; for example, you can remove the Minimize, Maximize, and Close buttons, or remove the System menu from the title bar of a form by setting the ControlBox property to False—useful if you want to ensure users cannot close the form unless they execute some code that closes it explicitly. Other properties apply to very specific circumstances; for example, the Opacity property can be used to control the level of transparency of the form. The following table describes some of the common form properties that you can change at design time. You should also be aware that there are additional properties not listed in the Properties window that you can use only programmatically at run time. For example, the ActiveControl property shows which control in the form currently has the focus.




The name of the form. Two forms in the same project cannot have the same name.


The default background color of any text and graphics in the form.


A bitmap, icon, or other graphic file to be used as a backdrop to the form. If the image is smaller than the form, it can be tiled to fill the form, stretched, centered, or zoomed by using the BackgroundImageLayout property.


The default font used by the controls embedded on the form that display text. This is a compound property—you can set many attributes of the font including the font name, size, and whether the font appears italic, bold, or underlined.


The default foreground color of any text and graphics in the form.


This controls the appearance and type of border of the form. The default setting is Sizable. Other options specify borders that are not resizable or do not have the various System menu buttons.


This specifies the icon that appears in the form's System menu and on the Microsoft Windows taskbar. You can create your own icons by using Visual Studio 2005.


This is another compound property that specifies the coordinates of the top left corner of the form with respect to its container, which might be another form or the screen.


This property specifies whether the Maximize command on the System menu and caption bar is enabled or disabled. By default, it is enabled.


This specifies the maximum size of the form. The default value (0, 0) indicates that there is no maximum size and the user can resize the form to any size.


This property is similar to the MaximizeBox property. It specifies whether the Minimize command on the System menu and title bar is enabled or disabled. By default, it is enabled.


This property specifies the minimum size of the form.


This is the default size of the form when it is first displayed.


This property contains the text that appears on the title bar of the form.


This property determines the initial state of the form when it is first displayed. The default state (Normal) positions the form according to the Location and Size properties. The other options are Minimized and Maximized.

You can view a summary of a property by selecting the property in the Properties window, right-clicking it, and then clicking Description. A pane displaying a description of any selected property appears at the bottom of the Properties window. Clicking Description again hides the description of the property.
Changing Properties Programmatically

In addition to setting properties statically at design time, you can write code that changes properties dynamically at run time. For example, you can change the Size property of a form in your program to make it shrink or grow without the user dragging the border to resize it. In actual fact, if you look at the code behind the form, you will see that Visual Studio 2005 generates code to change the properties of a form at run time according to the values you specify at design time. If you click the + sign adjacent to Form1.cs in the Solution Explorer you will see the file Form1.Designer.cs (you will also see Form1.resx which contains informa- tion about resources, such as bitmaps, used by your application). Right-click the file Form1.Designer.cs and click View Code to display the generated code. You already saw this code in Chapter 1, “Welcome to C#,” but now you can start to appreciate what it actually does.

In this code, you will notice that the form is simply a class that contains a private System.ComponentModel.IContainer variable, called components, and a Dispose method. IContainer is an interface that includes a collection for holding references to the components belonging to the form. The Dispose method implements the disposal pattern described in Chapter 13, “Using Garbage Collection and Resource Management,” to quickly release any unmanaged resources used by the form when it is closed.

Expanding the Windows Forms Designer generated code region reveals another method called InitializeComponent. If you expand this method, you can see how the property values you specified in the Properties window are translated into code. Later, when you add additional controls to the form, code will be inserted into this method to create them and set their properties as well. If you change the values in the Properties window, Visual Studio 2005 will keep the code in this method synchronized with your changes.

You should not modify the code in the InitializeComponent method, or anywhere else in the Windows Forms Designer-generated code region. If you do, the changes you make will likely be lost the next time any property values are amended in Design View.

You should notice that the code in Form1.Designer.cs is actually a partial class, used to separate the statements and methods generated by Visual Studio from your own code. In the Solution Explorer, right-click Form1.cs and then click View Code to display the file that you add your own methods and fields to. You will see that this file already contains a default constructor that simply calls the InitializeComponent method to create and layout the form at runtime.

Previous Page
Next Page