Now that a basic understanding of a Windows Forms application and the Windows Forms Designer have been established, it is time to create your first Windows Forms application. In the following sections, you will learn to create, compile and run a simple Hello World application.
The first step in creating this simple application is invoking the Windows Application Wizard from the Visual Studio .NET New Project dialog as shown in Figures 15.3 and 15.4.
When the New Project dialog window appears, select the Windows Application Wizard shown in Figure 15.4 and type in the name and location of the project that you want to create. After you've entered all the required information, click the OK button and watch Visual Studio .NET create your application, as shown in Figure 15.5.
You will notice that the Solution Explorer contains three files. One file (App.ico) is the application icon. This file contains the icon image that will be used in your application. Another file (AssemblyInfo.cs) contains all information about the assembly, such as signing and general assembly and version information. The final file (Form1.cs) contains the Web Form that is the basis of our application. By default, it is a blank form that has the name Form1.
Now that the basic application has been created, examine the Solution Explorer.
If you want to change any of the basic information of this project, including the name of the icon file to be associated with it, you can invoke the application property pages by right-clicking on the project file in the Solution Explorer and selecting the Properties menu item. From these pages, you can change properties in Tables 15.1 and 15.2.
Now that all the files that will be used in the project have been identified, the form must be loaded in the Design View window. To do that, simply right-click on the file in the Solution Explorer, and select the View Designer menu option.
TIP
Instead of right-clicking on the file and selecting the View Designer menu option to view the form in Design mode, you can simply double-click on the file and the Design View window will appear.
Next, drag and drop a Label control from the Windows Forms tab in the toolbox to the form. After the Label control has been placed on the form (as shown in Figure 15.7), change the control's Text property to Welcome to Windows Forms Development!.
While you are in the Properties window, change the font size from its default value to a larger font size of 24. You will notice that the label now extends past the end of the form. That's okay; resize the form by clicking the mouse cursor on the lower-right side of the form and dragging to the right until the entire Label control can be seen (see Figure 15.8).
After you have completed the visual design of the Hello World application, you must compile the application before it can be run. The compilation of a program can be accomplished in a few different ways: You can choose to compile the application from the keystroke Ctrl+Shift+B (assuming that you are using the default keystroke mapping); you can select the Build, Build <Project Name> menu option; or you can click the Build Solution toolbar button (by default, this button does not appear on the toolbar). Each of these techniques accomplishes the same thing, but seems faster than the other. Compile the program. Figure 15.9 shows the output of the compiler when it has successfully completed its task.
You have a few different options to run the application: You can press the F5 key; you can select the Debug, Start option from the main menu; or you can click the Start button on the toolbar. Using the F5 key seems faster than using the other options.
TIP
Instead of performing two different steps—compiling and then building the application—you can just click the Start button. If the application must be rebuilt, Visual Studio .NET will automatically perform this process. If the application does not require a rebuild, Visual Studio .NET simply starts it.
After the application has been started, you can see your welcome message, as shown in Figure 15.10.
The welcome message is a great start, but it is not very useful. In fact, displaying the message did not require any coding, so the example was perhaps too simple. In Chapter 10, “Events and Delegates,” you learned what an event was and why it is used. By default, a Button control has several events ready for your use (see Figure 15.11).
To add an event handler to any of the provided events, simply double-click the space next to the event name. The Forms Designer will automatically add the event handler, with a default name, in the Code View window and place your cursor inside the event handler method. All you have to do is add the code that you want to handle the event. To better illustrate this, add a Button control to the Hello World application form, as shown in Figure 15.12.
After the button has been added, add an event handler to the Button.Click event. When that has been accomplished, the Forms Designer should place your cursor in a method that resembles the following code snippet:
private void button1_Click(object sender, System.EventArgs e) { }
When that has been achieved, add the following line of code to this event handler:
MessageBox.Show("Button Clicked!");
The event handler should now look something like this:
private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show("Button Clicked!"); }
When the application is run, you will see the same welcome message with the addition of one Button control (see Figure 15.13).
If you click the button, you will see a MessageBox appear with the message Button Clicked!.
Listing 15.1 contains all the code necessary to create the Hello World application.
18.225.255.187