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

In addition to the Name property, you can assign a check mark to a menu item. This is often used if you want to indicate that something can be turned on or off, such as italic. You can also assign a shortcut such as a function key or control key combination to a menu item, and when the user assigns the shortcut, any code associated with that menu is triggered. This is a very easy way to add keyboard commands to your programs without having to call a keyboard handler explicitly. The Shortcut property of the MenuItem object has a drop−down list of legal keystroke commands you can associate with a menu item.

Menu item names often have one character underlined. If you press the Alt key and then the underlined letter (sometimes called a hotkey), control immediately jumps to the menu item. For example, many users save documents with the Alt+F+S combination. Alt+F calls the File menu in many programs, and the S key usually invokes the Save command on the File menu. To add an underscore (and the associated behavior) to a menu item, precede the character you want to underline with an ampersand. For example, change File to &File to underline the F key and have it act as the hotkey for the File menu.

Trick The system of hotkeys also works with command buttons. It can be an easy way to support those users who prefer to use the keyboard. Also, keyboard shortcuts such as the hotkey combinations can greatly simplify life for users with visual impairments or other disabilities, who find keyboard commands much easier to manage than mouse−based input. In any case, hotkey commands add a lot to your menu structure and are very easy to add to your programs.

Writing Event Code for Menus

Menus are almost always associated with some sort of event handling (except for the topmost menus, such as File and Count, although you could add some sort of event to them, if you wanted to). You add event handlers to a menu item just like any other control. Double−click the menu item to call up the default event handler, or choose another event from the Events list on the Properties window.

The following listing shows the event handlers for the Menu Demo program:

private void muEng1_Click(object sender, System.EventArgs e) { lblNumber.Text = "one";

}

private void mnuEng2_Click(object sender, System.EventArgs e) { lblNumber.Text = "two";

}

private void mnuEng3_Click(object sender, System.EventArgs e) { lblNumber.Text = "three";

}

private void mnuJapan1_Click(object sender, System.EventArgs e) { lblNumber.Text = "ichi";

}

private void mnuJapan2_Click(object sender, System.EventArgs e) { lblNumber.Text = "ni";

}

private void mnuJapan3_Click(object sender, System.EventArgs e) { lblNumber.Text = "san";

}

249

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