How to: Create a C# Windows Application
转自微软网站
The purpose of this topic is to acquaint you with elements of the Visual C# Express development environment as you build a relatively straightforward C# program using Windows Forms. Windows Forms provide your project with the components that make up a standard Windows application user interface, including dialog boxes, menus, buttons, and many other controls.
This example shows you how to create your own Web Browser application, which you can customize with shortcuts to your favorite Web sites.
Note:
Many of the features of the development environment discussed in this topic will also be encountered when developing console applications, so don't skip this part simply because you don't plan on writing Windows applications.
In this section, you'll learn:
How to create a new Windows application.
How to toggle between Code and Design views.
How to change the Windows Form's properties.
How to add a menu control.
How to create and populate a ComboBox control.
How to use a Web Browser control.
How to create handlers for controls.
To create a C# Windows application
1. Launch Visual C# Express.
2. Click New on the File menu, and then click Project.
The New Project dialog box appears. This dialog box lists the different default application types that Visual C# Express can create.
3. Select Windows Application as your project type.
4. Change the name of your application to Web Browser. The default location should be fine, but you can always enter a new path if you wish.
5. Click OK.
Visual C# Express creates a new folder for your project, named after the project title, and then opens the main Visual C# Express window. The default view is of your new Windows Form, entitled Form1. You can change from this view to the source code view at any time by pressing F7, and return to the designer view by pressing SHIFT + F7.
The Windows Form is the window that opens when your application is launched. You can drag various controls into this window, accept input from the user, display text and graphics, and do everything that a Windows application can do. Behind the scenes, the code that is required to manage the window has been created by Visual C# Express, and is contained in a file that is normally hidden from view. You can see this file — called Form1.designer.cs — if you open Solution Explorer, click on the Show All Files icon in Solution Explorer toolbar, and then expand Form1.cs. The file is hidden because usually the Visual C# Express designer manages the code it contains, rather than allowing you to edit it manually.
6. Change the size of the Windows Form.
If you aren't already in the designer view, press SHIFT + F7. Click on the bottom right corner of the Windows Form, and when the cursor becomes a double-headed arrow, click and pull the corner of the form until it's approximately as wide and as deep as a quarter of your screen. This is the window in which your Web sites will be displayed, so you don't want it be too cramped.
7. Change the title of the Windows Form.
If the Properties window isn't already open, select Properties Window from the View menu. This window lists the properties of the currently selected Windows Form or control, and it's here you can make alterations to existing values.
Change the Windows Form title from Form1 to Web Browser by finding the property Text, and changing the value in the column next to it. Once you confirm the change by pressing ENTER or the TAB key to move focus from the control, you'll see that the name on your Windows Form has changed. Notice that there is a similar field called (Name). This is the name of the class that contains the Windows Form. You can change it too, but if you do, remember that the rest of the code in this task refers to the old name of Form1.
To quickly change the name of a control without opening the Properties pane, right-click on the control, and select Edit Properties from the shortcut menu.
8. Add a Menu control.
Open the Toolbox by clicking the Toolbox icon in the toolbar, or by selecting Toolbox from the View menu. Scroll down the list of controls until you get to MainMenu. Click and drag this control to anywhere the Windows Form.
After you have released the menu control, you'll see that it creates a default menu at the top of the form.
9. Populate the Menu.
In the box that says Type Here, enter the name of the menu; in this case Navigate. When you press ENTER, new empty boxes appear to create other menus, and menu items. In the lower box, type Home. Press ENTER, and more boxes are displayed. Type Go Back. Press ENTER, and type Go Forward. These menu items form your basic Web site navigation controls.
In order to make your source code more legible, you should rename the default names given to the menu items. If you open the Properties window and click on each menu item in turn, you will see they have design names such as menuItem1, menuItem2 and so on.
Rename the menu items so that the Home item is called menuItem_Home, Go Back is called menuItem_GoBack, and Go Forward is called menuItem_GoForward.
10.Add a button.
From the Toolbox, drag a button control to approximately the middle of the Windows Form, just below the menu bar. View the buttons in the Properties window, change the Text property to Go instead of button1, and change the design name from button1 to button_go.
11.Add a ComboBox.
Drag a ComboBox control from the Toolbox and position it to the left of the new button. Click and drag the edges and corners to resize and reposition the ComboBox until it is lined up with the button as shown in the following screenshot.
Note:
When you are moving controls around a Windows Form, you will see blue lines appear. These lines are guides that help you line up the controls vertically and horizontally. You can also line up controls by selecting more than one at a time. You can do this by clicking and dragging a selection box around the controls, or holding down SHIFT as you click on them. Then apply the align and resize icons, which appear in the Layout Toolbar at the top of the Design window.
12.Populate the ComboBox.
A ComboBox provides a drop-down list of options from which the user can choose. In this program, the ComboBox is going to contain a list of your favorite Web sites for quick access.
To create the list of sites, select the ComboBox, and view its properties. Click in the column next to the Items property, and you'll see an ellipses button (…). Click on this button to edit the contents of the ComboBox. Add as many Web site URLs as you want, pressing RETURN after each.
13.Add the WebBrowser control.
Open the Toolbox, and scroll down until you locate the WebBrowser control. Click and drag the control to the Windows Form. Resize the WebBrowser control to fit nicely inside the Windows Form, without obscuring the ComboBox and Button controls. If the WebBrowser control doesn't resize easily, open its properties, locate the Dock setting, and make sure that it is set to none. Setting the Anchor settings to Top, Bottom, Left, Right and setting the AutoRelocate property to True will cause the WebBrowser control to resize properly when you resize the application window.
The WebBrowser control is the control that does all the hard work of rendering the web pages.
14.Add a handler for the Button control.
You have now finished the design stage of your application, and are at the point when you need to start adding some C# code to provide the program's functionality.
We need to add handlers for the button and for each of the menu options. A handler is a method that is executed when a particular control is activated. Visual C# Express creates empty handlers for you automatically.
Double-click on the button, and you'll see the Code editor for your project appear. You'll also see that the handler for the click event — the event message that occurs when the user clicks on a button — has been created for you. Add code to the handler method so it looks like the sample below.
Copy Code
private void button_go_Click(object sender, System.EventArgs e)
{
webBrowser1.Navigate(comboBox1.SelectedItem.ToString());
}
This code takes the currently selected item from the ComboBox control, a string containing a Web URL, and passes it to the Web Browser's navigation method. The navigation method loads and displays the contents of the Web page at that location.
15.Add handlers for the Menu options.
Return to the Design window by pressing F7, and double-click on each of the menu items in turn. Visual C# Express creates handler methods for each. Edit these methods so they look like the code shown below.
Copy Code
private void menuItem_Home_Click(object sender, System.EventArgs e)
{
// Go to Home Page menu option
webBrowser1.GoHome();
}
private void menuItem_GoForward_Click(object sender, System.EventArgs e)
{
// Go forward menu option
webBrowser1.GoForward();
}
private void menuItem_GoBack_Click(object sender, System.EventArgs e)
{
// Go back manu option
webBrowser1.GoBack();
}
Each of these menu handlers calls a navigation method that is part of the Web Browser class.
Note:
You can see from this code that the default names given to the menu options can become quite confusing. For this reason, it's a good idea to change the name of each menu control as you create it, using the Properties editor. The name of the handler will then reflect the name of the menu option.
16.Add some initialization code.
The last task is to add some more code to the Form1 method. This method is the constructor for the Form1 object, and is called when the Windows Form is being created. It's therefore the ideal location to place any code that is required to change controls' initial values or other settings.
The following code will cause the Web Browser control to display your computer's default home page, and also set the initial value of the ComboBox. Change the method to contain the following code:
Copy Code
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
webBrowser1.GoHome();
}
17.Build and run the program.
Press F5 to build and run your Web browser. Your Windows Form should be displayed on the screen, and then display your computer's default home page. You can use the ComboBox control to select a Web site, and click on Go to navigate to it. The menu options enable you to return home, or move back and forth through previously visited Web sites.
If you are new to C# programming, this would be a good time to read the C# Language Primer section. If you wish to know more about the Visual C# Express development environment, and how to create Console Applications using IntelliSense in particular, read the previous section How to: Create a C# Console Application.