Chapter 13: Discovering the Menu


Our addition of the section feature worked fine, except for an irritating beep every time we insert a section. A bit of learning suggests that this is a menu- related problem, even though we don t have a menu ”yet.

Exploring the Problem

As soon as we added the new section logic to the GUI, using Alt+S, and began using the software through the GUI, Chet and I discovered an irritating beep every time we typed the Alt+S. We tried to be sure that we were properly telling Microsoft Windows that we had consumed the character, but that didn t suppress the beep. So we did a little experimenting: we added a couple of Console.WriteLine sends to the KeyPress and KeyDown handlers, to print out what was happening and when. In the course of that, we learned some interesting things. For example, all typed characters seem to go through KeyDown, but some never show up in KeyPress. It isn t too much of a surprise to find out that Alt and Shift do that, since they are basically key modifiers. It s more interesting to know that the arrow keys ”up, down, left, right ”also do not show up in KeyPress. We ll have to remember that in case we want to specialize them.

Perhaps the most interesting thing, however, is the behavior of the Alt key. I noticed right away that it repeats in KeyDown until you type the S or whatever you re trying to Alt. No surprise. But the code I was using was this:

 Console.WriteLine( 
"KeyPress\n char {0}\n int {1}",
kea.KeyChar, (int)
kea.KeyChar);

What prints as the character? The Alt key is named Menu . Furthermore, when I typed one Alt, the screen displayed this:

 KeyDown  char Menu int 18 

But the second Alt you type doesn t print. Then the third one does, and so on. So, Holmes on the case here finally makes the connection: Alt is doing something about going to the Form s menu.

Therefore, I finally figured out, if the form had a menu item whose accelerator key was Alt+S, the menu item would trigger and probably that annoying beep would go away. I decided to do a little research on Menu. In my reading, I found Charles Petzold telling me that Microsoft .NET has changed the conventions a bit for how menus work, compared to conventional Microsoft Windows. I ll not go into details here, but it was enough to make me sure that trying the menu was probably interesting.




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net