Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2008

Tip: Looking for answers? Try searching our database.

How to trap F1 from a menu item

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johnny - 11 Jan 2008 22:49 GMT
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.
Bob Powell [MVP] - 12 Jan 2008 12:19 GMT
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.

Signature

--
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.

> Hi there,
>
[quoted text clipped - 5 lines]
> bring help up, I just can't figure out how to trap F1 itself. Thanks very
> much.
Johnny - 12 Jan 2008 14:32 GMT
>I don't think this is possible.
>
[quoted text clipped - 6 lines]
> 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.
Bob Powell [MVP] - 12 Jan 2008 16:49 GMT
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.

Signature

--
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.
>>
[quoted text clipped - 26 lines]
> particular has often been a very painful experience. In any case, thanks
> again for your help.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.