Handling Menus/Toolbars/ContextMenus

  • Thread starter Thread starter Matthias S.
  • Start date Start date
M

Matthias S.

Hi,

I'm looking for a decent way to manage Commands in my Application. As in
many applications, a Command can be triggered through various actions
(Click on the Menu, Click on the ContextMenu, Click on the Toolbar, using a
Shortcut).

Is there a common pattern used for managing the creation and state of your
commands, i.e. their visibility, whether they are enabled or not, etc.?

Any opinions, further readings?

Thanks in advance.
 
Hi Matthias,

I had a same situation in one of my previous project - we had the same
commads that could be triggered from either a toolbar button, a menu item or
other buttons available in the form.

The solution we came up with was this: we used the tag property of the
controls to store a command 'name'. And we set the event handler of each of
these controls to the same method - say 'HandleUserEvent'. From this method,
we would get the tag value of the control and then call another 'DoAction'
method passing the command string. This DoAction method would centrally
manage all the actions to be carried out depending upon the command name.

But note that the MenuItem object doesn't have the tag property as it
doesn't derive from the Control class. To handle that you could store all the
menu items in a hashtable and then store their corresponding command name.
It's just a matter of retrieving that value in the HandleUserEvent method.

You could easily handle the visibility of the controls in the DoAction method.

Will this help?

If you want to handle Undo operations, there is a design pattern on those
lines...but I don't remember the link to that...will let you know.

HTH,
Rakesh Rajan
 
Hi,

There is the Command pattern, search the MSDN magazine
msdn.microsoft.com/msdnmag/ I do remember one article about this in the mar
or apr issue from last year ( or the year before :) )

cheers,
 
Back
Top