Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

However, all those techniques share the basic file manipulation ideas presented here as their genesis.

Creating Menus

Menus have become a staple GUI component. Programmers like menus because they allow access to large numbers of commands and methods without requiring much space on the screen. Users like menus because they are predictable and allow access to much of the program's functionality (if they are written well, that is!).

The .NET IDE makes building a menu structure for your programs very easy. Menus are basically composed of two classes. The MainMenu class can be dropped on your form, and it provides the basic foundation of your menu system. The user doesn’t interact with the MainMenu class itself. Instead, the MainMenu has a series of MenuItem components. These MenuItems are the parts of a menu that are visible to the user.

Exploring the Menu Demo Program

The Menu Demo program consists of a simple form using GUI menus. Figures 9.9 and 9.10 show the completed program. Because users already know how to use a menu, you don’t have to teach this skill. However, it’s up to you to design your menus so that they are familiar to users. For example, most programs have a File menu, followed by an Edit menu. Users expect to find saving and loading commands on the File menu and copying commands on the Edit menu. If you don’t follow these conventions, expect some confusion from your users.

Figure 9.9: The form has one main menu, which has menu items. The menu items have submenus.

245

Figure 9.10: After the user makes a selection, the program prints out the number name.

Adding a MainMenu Object

Adding a menu to a GUI form is easy. Select the MainMenu control from the Toolbox on the left of the IDE screen, and drag it onto the form. The MainMenu control places itself in the area immediately below the form (where the timer and the image list go). You won’t see the MainMenu control on the form, but if you look in the upper−left corner, you will see that a MenuItem has been placed on the form for you. Figure 9.11 shows a form with one menu being placed on it.

Figure 9.11: The MainMenu object is below the form, and the first MenuItem object has been placed at the top of the window. (It says Type Here.)

It’s important to note that MainMenu and MenuItem are two different objects. MainMenu is used to attach menus to your form. After you add MainMenu, you don’t need to think much about it. The MenuItem class is used to generate all the items on the menu. The menus themselves (such as the

246

File menu and Edit menu) are MenuItems. Every element in the menu is also a MenuItem.

Adding a Submenu

To make a submenu, type in the Type Here box. I want to re−create the Menu Demo program shown at the beginning of the chapter, which has a File menu and a Count menu. The leftmost menu is File, so you type the word File in the Type Here box. Figure 9.12 shows what happens when you start to type in the box.

Figure 9.12: When you start to type in a menu item, two new potential menu items appear!

As soon as you begin to type code in a menu item, two new Type Here boxes appear. These are placeholders for future menu items. When you finish typing

247

Figure 9.13: Every element opens up the possibility for two more elements.

File in the upper−left box, you add Exit below it and Count to the left. Figure 9.13 shows what happens after I added a few more elements.

Trick You can create very complicated menu structures, if you like. Keep in mind, though, psychologists’ observations that most people can handle only seven options at a time. Subdividing your menus into reasonable chunks is smart, but menu structures 12 layers deep can be frustrating. As an example, look at the menu structure for Visual Studio itself. You can use the program for years and still not know where everything is on that behemoth. (In fact, you can even change the menu structure if you want, adding new custom commands and deleting things willy−nilly. This is a setup for a great April Fool’s joke at the office but makes the interface even more confusing than it already is.) The point of a menu structure is to make your program easy to use. Keep your menus simple and direct.

Setting Up the Properties of Menu Items

Menu items are objects, and they have properties, just like text boxes, labels, and all the other elements you place on forms. Like those other elements, the MenuItem class has properties that add functionality to the method. The most important property to change in a menu item is the name. The default names for menu items are not clear, and you usually have many of them if you have any at all. Your code will soon become very confusing if you start to write event code for the menu items without changing their names.

Trap With all controls that will have event handlers, it’s critical to change the object’s name before you assign an event handler to it (by double−clicking the element or choosing the event from the event list). If you assign an event and then change the object’s name, the event will still occur, but the event handler will refer to the old object name, not the new one. With menu events, this is even more critical than with other kinds of events because all the menus have very similar default names and there are often many menu items in a program.

248

Соседние файлы в предмете Программирование на C++