Section 13.1. Creating a Simple Windows Form


13.1. Creating a Simple Windows Form

A Windows Form is a tool for building a Windows application. The .NET Framework offers extensive support for Windows application development, the centerpiece of which is the Windows Forms framework. Not surprisingly, Windows Forms use the metaphor of a form. This idea was borrowed from the wildly successful VB environment and supports RAD. Arguably, C# is the first development environment to marry the RAD tools of VB with the object-oriented and high-performance characteristics of a C-family language.

13.1.1. Using the Visual Studio Designer

While it is possible to build a Windows application using any editor (even Notepad!) and compiling from the command line, it is senseless to do so, when Visual Studio.NET makes life so much easier.

To begin work on a new Windows application, first open Visual Studio and choose File New Project. In the New Project window, create a new C# Windows application and name it ProgCSharpWindowsForm, as shown in Figure 13-1.

Figure 13-1. Creating a Windows Form application


Visual Studio responds by creating a Windows Form application and, best of all, putting you into a design environment, as shown in Figure 13-2.

Figure 13-2. The design environment


The Design window displays a blank Windows Form (Form1). A Toolbox window is also available, with a selection of Windows widgets and controls. If the Toolbox is not displayed, try clicking the word "Toolbox," or selecting View Toolbox on the Visual Studio menu. You can also use the keyboard shortcut Ctrl-Alt-X to display the Toolbox.[1]

[1] Visual Studio allows a great deal of personalization; please check all the keyboard shortcuts to ensure that they work as expected in your environment.

With the Toolbox displayed, you can drag a label and a button directly onto the form, as shown in Figure 13-3.

Figure 13-3. The Windows Form development environment


Before proceeding, take a look around. The Toolbox is filled with controls that you can add to your Windows Form application. In the upper-right corner, you should see the Solution Explorer, a window that displays all the files in your projects. In the lower-right corner is the Properties window, which displays all the properties of the currently selected item. In Figure 13-3, the label (label1) is selected, and the Properties window displays its properties.

You can use the Properties window to set the properties of the various controls. For example, to add text to label1, you can type the words "Hello World" into the box to the right of its Text property. If you want to change the font for the lettering in the HelloWorld label, click the Font property shown in the lower-right corner of Figure 13-4. (You can provide text in the same way for your buttonbutton1by selecting it in the Properties window and typing the word "Cancel" into its Text property.)

Figure 13-4. Modifying the font


Any one of these steps is often easier than modifying these properties in code (though that is certainly possible).

Once you have the form laid out the way you want, all that remains is to create the click handler for the Cancel button. Double-clicking the Cancel button will create the event handler, register it, and put you on the code page (the page that holds the source code for this form), in which you can enter the event-handling logic, as shown in Figure 13-5.

Figure 13-5. After double-clicking the Cancel button


The cursor is already in place; you have only to enter one line of code:

Application.Exit();

You'll find that as you try to enter this code, Intellisense tries to help you. When you type A, the first possible object that begins with A is shown. Continue typing through Appl and then hit the period: the class Application is filled in for you,[2] and the methods and properties of the Application object are available. Type Ex and then type the semicolon. Hey! Presto! Your line of code is written.

[2] Note that Intellisense will remember your most recent choice and start with that; usually this is a great convenience.

In the IDE, the cursor flashes, making it easy to see where the code goes. For most readers, the cursor probably won't flash in this book.


Visual Studio generates all code necessary to create and initialize the components.

Note that your code file (Form1.cs) has only the using directives and the constructor and event handler. Those of you who programmed in previous versions of C# may be wondering where the rest of the code is to initialize and set the properties of your controls (which aren't even listed here!). Note, however, that the class definition contains the keyword partial. This indicates that the rest of the class definition is contained in another file. If you click the Show All Files button (circled in Figure 13-6), you will see that the designer has added another file, Form1.Designer.cs.

Figure 13-6. Showing all files


The file Form1.Designer.cs has all the code generated by Visual Studio.



Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net