« Home « Kết quả tìm kiếm

Adding Start-Processing Menu Collection


Tóm tắt Xem thử

- You can use the Visual Studio 2005 integrated development environment (IDE) and the menu editor to create a menu graphically.
- MenuStrip object (MenuStrip is a class defined in the Windows Forms library), and then add ToolStripMenuItem objects to it (ToolStripMenuItem is another class in the.
- In the following exercise, you will use the graphical approach to create menus for the Middleshire Bell Ringers Association application.
- Open the BellRingers project, located in the \Microsoft Press\Visual CSharp Step by Step\Chapter 21\BellRingers folder in your My Documents folder.
- 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)..
- In the Toolbox, expand the Menus &.
- Be sure to drop the MenuStrip control onto the form, and not onto a control on the form.
- 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..
- 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 &.
- 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..
- Click the Type Here caption under the New menu item, type &Open, and then press Enter..
- Click the menu item, and then use the Properties window to change the Text property of the item to the correct value..
- Click the Type Here caption under the Open menu item, type &Save Member, and then press Enter..
- Click the Type Here caption under the Save Member menu item, type a minus sign.
- and then press Enter..
- Click the Type Here caption under the separator bar, type &Print, and then press Enter..
- Click the Type Here caption under the Print menu item, type a minus sign.
- 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:.
- If you click this drop down, you will see three items: MenuItem, ComboBox, and TextBox, as shown in the following graphic:.
- The combo box and text box controls in a MenuStrip behave in a similar manner to ComboBox and TextBox controls on a form, and you can set their properties in the same way that you saw in Chapter 20.
- Setting Menu Item Properties.
- In the following exercise, you will set the properties of the MainMenu control and the menu items.
- Set the menu item properties.
- 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.).
- 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..
- Notice the names generated for each menu item.
- They should be the same as those listed in the following table.
- If not, click the menu item and then change the.
- (Name) property using the properties displayed on the right-hand side of the Items Collection Editor..
- The access keys for menu items (such as Alt+X for the Exit command) are available only when the menu is actually displayed.
- You can add a shortcut key to a menu item by setting the ShortcutKeys property..
- ShortcutKeys in the list of properties.
- This property determines whether the shortcut key is displayed on the menu when the application runs.
- Change the shortcut keys for the other menu items using the values in the following table.
- Ensure that the ShowShortcut property for each menu item remains set to True..
- Menu items can be enabled and disabled (disabled items are unavailable and their text appears dimmed) depending on the current actions and context.
- For example, in the Middleshire Bell Ringers Association application, it would not make sense to allow the user to use the Save Member or Print commands if there is no data to save or print..
- Select the printToolStripMenuItem item, and then click Enabled in the list of proper- ties.
- Test the menu.
- On the Debug menu, click Start Without Debugging to compile and run the application..
- When the form appears, click the File menu..
- Close the form.
- You can't use the Exit command on the File menu yet.
- click the Close button (the ‘X’ button in the top right-hand corner of the form) instead..
- Other Menu Item Properties.
- If you want more information about the other properties, consult the documentation in the MSDN Library for Visual Studio 2005..
- You can set menu item properties using the Items Collection Editor as described in the.
- previous exercises, or by clicking a menu item in the Designer View window and then using the Properties window..
- (Name) This property is the name of the menu item..
- CheckOnClick Setting the CheckOnClick property to True causes the menu item to be checked and unchecked automatically when the user clicks it..
- If you set the DisplayStyle property to the value Image or ImageAndText, the image specified by the Image property will appear on the menu..
- This property specifies whether the menu item is enabled or disabled.
- If a menu item is not enabled, it appears dimmed and the user will not be able to select it..
- ShowShortcut If this property is True, the shortcut key is displayed on the menu alongside the text of the menu item..
- Text This is the text that is displayed on the menu for the menu item.
- ToolTipText This property specifies the text to display as a tool tip when the user hovers the mouse over the menu item..
- This property determines whether the item should be displayed on the menu.
- It is more common to use the Enabled property to show that a menu item is present but unavailable..
- There are several different events that can occur when a user gains access to a menu item..
- The most frequently used event is the Click event, which occurs when the user clicks the menu item.
- You typically trap this event to perform the tasks associated with the menu item..
- In the following exercise, you will learn more about menu events and how to process them.
- Therefore, until the user clicks New, all fields on the form should be disabled.
- When the user clicks the New command on the File menu, you want to enable all the fields, reset the contents of MemberForm so that the user can start adding information about a new member, and enable the Print command..
- Handle menu item events.
- In the MemberForm in the Design View window, click the firstName text box.
- In the Properties window, set the Enabled property to False.
- Click the File menu, and then click New..
- In the Properties window, click the Events button.
- A new event method is created and the source code displayed in the Code And Text Editor window..
- In the body of the newClick event method, type the following statement:.
- If you remember in Chapter 20, the Reset method resets the controls on the form to their default values.
- After the call to the Reset method in the newClick event method, add the following statements:.
- You must also enable the controls on the form.
- This method should cause the form to close.
- MemberForm, and then on the File menu, click Exit..
- In the Properties window, verify that the events are displayed and select the Click event.
- The exitClick event method is created and the source code displayed in the Code And Text Editor window..
- In the body of the exitClick method, type the following statement:.
- The Close method of a form attempts to close the form.
- If the user says no, the form does not close and the application continues to run..
- Test the menu events.
- Notice that all the fields on the form are disabled..
- Click the File menu..
- Click the File menu again..
- The Print and Save commands are now enabled, as are all the fields on the form..
- The form tries to close.
- You are asked if you are sure you want to close the form.
- If you click No, the form remains open.
- if you click Yes, the form closes and the application finishes..
- Click Yes to close the form.

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt