13.1. Creating a Simple Windows FormA 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 DesignerWhile 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 applicationVisual 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 environmentThe 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]
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 environmentBefore 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 fontAny 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 buttonThe 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.
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 filesThe file Form1.Designer.cs has all the code generated by Visual Studio. |