ShortcutKeys for ToolStripMenuItem

Topics: CAB & Smart Client Software Factory
Apr 17, 2006 at 9:56 PM
originally posted by: mojograz

Hi,

I read a lot of discussion concerning commands and UIExtensions in
this forums but what I am missing is an issue that I ran across.
I have many different work items in my application, each of them
manipulating and storing data in its own way. What I want to achieve
is that there is one single File->Save menu item with the common
ShortCutKeys Ctrl+S.
Perhaps I am missing something but if I register many different
menu items using these ShortcutKeys showing or hiding them depending
on their work items, only the first item can be used with the short cut.

Has anyone else experienced this and what was your solution of the
problem?

Regards,
Jochen
Apr 28, 2008 at 2:00 AM
I've just come across this exact problem.

My individual work items need their own Save menu item and each uses Command.Status to show or hide its menu. However, the last added menu (first appearing in the File menu drop down) is the only one that responds to Ctrl+S.

Any solutions out there?

Thanks.
Apr 28, 2008 at 2:58 PM
Having only one Save item will work, you just have to track the active view.

We track our active views with a service. Each time we go to show a view in a Workspace, we follow that up with a call to the Service to set the active view. In our CommandHandlers for menu items and tool strip items, we check to make sure the view is the active view (once again, querying the service).

Apr 28, 2008 at 7:27 PM
Hi,

One possible workaround is to override the ProcessCmdKey(ref Message msg, Keys keyData) method of the ShellForm. Then, check the keys combination and publish a global event. I.e.:
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys)Shortcut.CtrlS)
                OnSave(new EventArgs());
 
            return base.ProcessCmdKey(ref msg, keyData);
        }
 
        protected virtual void OnSave(EventArgs eventArgs)
        {
            if (Save != null)
            {
                Save(this, eventArgs);
            }
        }
 
        [EventPublication(EventTopicNames.Save, PublicationScope.Global)]
        public event EventHandler<EventArgs> Save;
Once this is done, you could just add subscriptions to this event in your different modules and have them check if the Command corresponding to the ToolStripMenuItem is Enabled, and if so, call the Command.Execute() method. I.e.:
        [EventSubscription(EventTopicNames.Save)]
        public void OnSave(object sender, EventArgs e)
        {
            Command command = WorkItem.Commands["SaveOnModule1"];
 
            if (command.Status == CommandStatus.Enabled)
                command.Execute();
        }
Hope it helps.

Ignacio Baumann Fonay
http://staff.southworks.net/blogs/ibaumann/