CheckState of Menuitems and Commands

Topics: CAB & Smart Client Software Factory
Aug 18, 2006 at 12:20 AM
originally posted by: unassal

Is there a way to deal with the checkstate of menu items and the corresponding command state? I cannot see how the command states can be mapped to the checked state ...

Any suggestions/ideas?

Uwe
Aug 18, 2006 at 1:04 AM
originally posted by: hmoeller

I came across the same problem. Unfotunately, I wasn't able to figure a CAB-consistent solution to this issue. I've found a workaround, though, which may help. The code snippet below allows to access the associated menu item for a given command name. This way, you can alter the CheckState etc. of the menu item corresponding to the command state.

private ToolStripMenuItem GetCorrespondingMenuItem(string aCommandName)
{
ToolStripMenuItem menuItem = null;

Command command = _workItem.CommandsaCommandName;

ReadOnlyCollection<ToolStripItemCommandAdapter> coll = command.FindAdapters<ToolStripItemCommandAdapter>();
foreach (ToolStripItemCommandAdapter commandAdapter in coll)
{
foreach (object obj in commandAdapter.Invokers.Keys)
{
if (obj is ToolStripMenuItem)
{
menuItem = (obj as ToolStripMenuItem);
}
}
}

return menuItem;
}

But maybe someone else is able to provide some more convenient solution.
Aug 18, 2006 at 4:44 AM
originally posted by: PJackson

We created a custom command class that keeps its invoker's checked state in synch -- thus working if a toolbar button and menu item are both tied to the command and if the command is executed from code rather than the invoker.

I'm not in the office today to post a code sample, but will tomorrow.