OwnerDraw ContextMenu mnemonics

JamieB

Newcomer
Joined
Jan 10, 2006
Messages
5
Location
UK
I need to write a menu control and was hoping to base it on ContextMenu, owner-drawn to conform with my GUI style. My problem is that the mnemonic keys do not work when OwnerDraw is true. In itself that wouldn't be too bad if I could capture key press events but I can't find a way to do that either whilst the menu is displayed. Does anybody know a way around this?
 
Mnemonics seem to work for me with owner drawn context menus.

It might be worth mentioning that there is an overload for DrawString which takes a StringFormat object which allows you to render text with underlined mnemonics.
C#:
MyStringFormat.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
Visual Basic:
MyStringFormat.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show
 
Thanks, I did that. The mnemonics are underlined and the debugger shows the Mnemonic properties are set, but pressing the corresponding keys on the keyboard has no effect. If I use the same code with a MainMenu it works ok.

I've attached some sample code in case it helps.
 

Attachments

Here is the difference. When I tried it, I had the control automatically display the context menu by setting the control's ContextMenu property to my context menu. In your code, you explicitly call the ContextMenu.Show method. This shouldn't make a difference, but appearently it does.

If possible, set your context menu to be displayed automatically by setting it as a control's context menu (don't know if this is a 2.0 only feature or what).
 
You are right - it does work when I set the Form's ContextMenu property. And I suspect that indeed it shouldn't make a difference. Hmm... I was planning to show menus in response to certain key presses in my app, which is required to run 'mouseless', so I need a bit of a rethink there.

Anyway, many thanks for your help!
 
Back
Top