分享
 
 
 

How to: Create a C# Windows Application

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有