How to trap F1 from a menu item

  • Thread starter Thread starter Johnny
  • Start date Start date
J

Johnny

Hi there,

Does anyone know how I can trap F1 to activate help when a menu item is
active, preferably without creating a "ToolStripMenuItem" derivative (so I
can quickly wire this up using the designer). I'm doing this when my users
activate a menu from my main form. I simply want to activate help based on
the current menu item at the time F1 is pressed. Note that I know how to
bring help up, I just can't figure out how to trap F1 itself. Thanks very
much.
 
I don't think this is possible.

The menustrip eats all of the keypresses and attaching to the various
keypress event handlers, processcmdkey of the form and key preview events
doesn't work. Niether do the various help, queryaccessibilityhelp etc etc
events of the toolstrip menu items. (one wonders why these events were
provided in the first place)

The only way will be to derive from the menustrip and then you'll have
problems at design time.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
I don't think this is possible.
The menustrip eats all of the keypresses and attaching to the various
keypress event handlers, processcmdkey of the form and key preview events
doesn't work. Niether do the various help, queryaccessibilityhelp etc etc
events of the toolstrip menu items. (one wonders why these events were
provided in the first place)

The only way will be to derive from the menustrip and then you'll have
problems at design time.

Thanks for the info. Since posting however I came across the
"ToolStripRenderer" class and derivatives. By deriving my own class from
"ToolStripSystemRenderer" in particular and assigning an instance to
"ToolStripManager.Renderer", I can now override
"OnRenderMenuItemBackground()" and trap whenever a menu item becomes active.
By wiring all this into my own "Form" deriviative ("FormEx" which all forms
in my app derive from), I can now provide a protected function called
"FormEx.OnMenuItemEnter()". This fires my own "MenuItemEnter" event (to keep
things consistent with the native .NET. classes), but it also set a property
called "FormEx.CurrentMenuItem". The bottom line is that.all my "FormEx"
derivatives now have access to this property which will be null if no menu
item is currently active. Now my form itself can catch keystrokes as usual
(including F1) and check this property to determine which menu item is
currently active (if any). In any case, having to turn to such unorthodox
techniques is ugly to say the least but at least the plumbing is hidden from
users of my "FormEx" class. I find my self constantly struggling with issues
like this however. Trapping keys in particular has often been a very painful
experience. In any case, thanks again for your help.
 
Trapping keystrokes has always been a total pain in Windows Forms. Event
bubbling in WPF seems to be a much nicer way of doing things.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top