Danh mục

Adding Menus and Processing Menu Events

Số trang: 9      Loại file: pdf      Dung lượng: 24.22 KB      Lượt xem: 2      Lượt tải: 0    
thaipvcb

Hỗ trợ phí lưu trữ khi tải xuống: 1,000 VND Tải xuống file đầy đủ (9 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Thêm Menus và chế biến Menu Sự kiện Microsoft Visual Studio 2005 cho phép bạn thêm các trình đơn và các mục trình đơn mẫu theo hai cách. Bạn có thể sử dụng Visual Studio 2005 tích hợp môi trường phát triển (IDE) và biên tập các trình đơn để tạo ra một menu đồ họa.
Nội dung trích xuất từ tài liệu:
Adding Menus and Processing Menu Events Adding Menus and Processing Menu EventsMicrosoft Visual Studio 2005 lets you add menus and menu items to a form in two ways.You can use the Visual Studio 2005 integrated development environment (IDE) and themenu editor to create a menu graphically. You can also write code that creates aMenuStrip object (MenuStrip is a class defined in the Windows Forms library), and thenadd ToolStripMenuItem objects to it (ToolStripMenuItem is another class in theWindows Forms library).Laying out a menu is only half of the story. When a user clicks a command on a menu,the user expects something to happen! You activate the commands by trapping menuevents and executing code in much the same way as you do for handling control events.Creating a MenuIn the following exercise, you will use the graphical approach to create menus for theMiddleshire Bell Ringers Association application. We will cover how to manipulate andcreate menus programmatically later in this chapter.Create the File menu 1. Start Visual Studio 2005. 2. Open the BellRingers project, located in the \Microsoft Press\Visual CSharp Step by Step\Chapter 21\BellRingers folder in your My Documents folder. This is the Middleshire Bell Ringers application. It should be the same as the version that you completed in Chapter 20. 3. Display MemberForm in the Design View window. (Click MemberForm.cs in the Solution Explorer, and then click Designer on the View menu, or double-click MemberForm.cs in the Solution Explorer). MemberForm appears. 4. In the Toolbox, expand the Menus & Toolbars category. Drag a MenuStrip control anywhere onto MemberForm. IMPORTANT Be sure to drop the MenuStrip control onto the form, and not onto a control on the form. Some controls, such as GroupBox, can have their own menus, and dropping a menu on such a control will create a menu for that control only. The control, by default called menuStrip1, appears at the bottom of the form, and a menu strip containing the caption, Type Here, is added to the top of the form.5. Click the Type Here caption on the menu strip, type &File, and then press Enter. (If the Type Here caption on the menu strip disappears, click the mainStrip1 control at the bottom of the form and the menu reappear.) When you click the Type Here caption, a second Type Here caption appears to the right of the current item, and a third Type Here caption appears under the File menu item. TIP The & character in front of a letter provides fast access to that menu item when the user presses the Alt key and the letter following the & (in this case, Alt+F). This is another common convention. When you press the Alt key, the F at the start of File appears with an underscore. Do not use the same access key more than once on any menu because you will confuse the user (and probably the application).6. Click the Type Here caption that appears under the File menu item, type &New, and then press Enter. Another Type Here caption appears under the New menu item.7. Click the Type Here caption under the New menu item, type &Open, and then press Enter. TIP If you mistype the text for a menu item, you can easily correct it. Click the menu item, and then use the Properties window to change the Text property of the item to the correct value.8. Click the Type Here caption under the Open menu item, type &Save Member, and then press Enter.9. Click the Type Here caption under the Save Member menu item, type a minus sign (–), and then press Enter. The minus sign appears as a menu separator bar used to group related menu items together.10. Click the Type Here caption under the separator bar, type &Print, and then press Enter.11. Click the Type Here caption under the Print menu item, type a minus sign (–), and then press Enter. Another separator bar appears. 12. Click the Type Here caption under the second separator bar, type E&xit, and then press Enter. Notice that the conventional access key for exit is “x.” When you have finished, the menu should look like this:Menu Strip Item TypesYou have been using MenuItem controls, which are the default item type for MenuStripobjects. However, you can also add combo boxes and text boxes to menus in VisualStudio 2005 applications. You might have noticed that a drop-down arrow appears whenhovering the mouse over the Type Here caption in a MenuStrip control. If you click thisdrop down, you will see three items: MenuItem, ComboBox, and TextBox, as shown inthe following graphic:The combo box and text box controls in a MenuStrip behave in a similar manner toComboBox and TextBox controls on a form, and you can set their properties in the sameway that you saw in Chapter 20. However, strictly speaking they are different from theversions of the controls that you drop onto forms. They are ToolStripComboBox andToolStripTextBox controls, and are designed and optimized specifically for use inMenuStrip controls, and other related “strip” controls, such as the ContextMenuStrip andToolStrip controls.Setting Menu Item PropertiesIn the following exercise, you will set the properties of the MainMenu control and themenu items. You will use the Properties window to perform these tasks.Set the menu item properties 1. Click the menuStrip1 control under the form. In the Properties window, change its name to mainMenu. (If the Properties window is not displayed, click Properties Window on the View menu, or press F4.) 2. Right-click the File menu item on the menu strip of MemberForm. On the menu that appears, click Edit DropDownItems. The Items Collection Editor displays the names of the menu items as well as their properties. TIP You can use the Items Collection ...

Tài liệu được xem nhiều: