How to handle menus?


Robert Schneider

I'm creating an application that has should have a style like VS.NET.

What I would like to know is what is the best way to handle menus, tool bars
and context menus. How can this be done? The menus are dynamic so they have
to be determined with each click. But how does it work?

Let's take VS itself: If I single-click in the project view on a file node
some things happens. I.e. the properties view is refreshed. But also the
menu and the tool bars. If I even right click on the node a context menu
appears. What do you think how this works?

Is there a selection event that is received by the 'main app' which then
determines which menu and tool bar items to show? Or are those items
provided by the project view which gets merged into the main menu and tool
bars somehow?

And where is the logic which determines which items has to be enabled or
checked or visualized? Is there one intelligent class or package that
controls that for the whole application? Conside that I select a file node,
a project node and a folder at the same time. Who or what determines the
available commands?

Consider also that I would like to establis a MVC architecture. So I have
commands somewhere (with which I'm confused as well, how to organize them)
and I have the graphical representation of those. As menu entries, tool bar
items or context menu items.

Thanks in advance,





In short, menus can be as simple or as complex as you want them to be.
First, the menu architecture should be thought out and planned up
front. Then decide how simple or complex your menus need to be.

For simple menus, say, where you have defined two context menus (menuA
and menuB) that each contain a separate list of menu items, you may
show depending on the "sender" of the click. The object that is clicked
is generally evaluated as the "sender" of the click and usually
determines what context menu you want to display.

In the case of how menus work in Visual Studio, they are more complex.
For simplicity, let's think about the different menuItems that appear
in context menus that appear when you right-click different objects in
the VS designer. When the click happens, the sender is most likely
evaluated first to determine the menu "base" that will be displayed.
But, during the click event processing other aspects of the designer
may be interrogated to determine what other menuItems should appear in
the base menu. These could be singular menuItems or another predefined
menu that may be "merged" with the base.

When menus become complex, it may be helpful to create your own menu
classes that inherit the menu functionality but have extended
processing capabilities to make the work a bit easier.

For graphics added to the menu items, you may want to consider adding
graphics references to a resource file in your project to manage
images, icons, sounds, etc.

Resource info:
-> Lutz's resource handy resource editor:
(You can Google the net for other resource example sites as it can be
tricky to get setup properly).



I'd recommend you read the following short article that discusses command

These two short articles may help illustrate the basic idea as well:

(and for .Net 2.0 only)

In the end, the time spent writing a class to manage your application's
commands is well spent.

If you provide an email address, I can send you my draft version (for .Net
2.0) ...which is already more comprehensive than those above (it handles
shortcut keys, images, checked, enabled, visible, etc.) and implements
IExtenderProvider as well to enable designtime configuration. I plan to
write a full article using my class as an example but there's never enough
hours in the day :)




Robert Schneider

Thank you both for the answeres.
I'd recommend you read the following short article that discusses command

I already know this article. The other ones you have provided dealing about
the same. I think I understand how to realize the gui stuff for the
commands. In addition I will use for those things a third party library.

However, my problem is more about the control of the commands. E.g. what is
invoking CommandA.Enabled = true? And what has evaluated that it the
CommandA has to be enabled?

Did you relate to this when you write:
In the end, the time spent writing a class to manage your application's
commands is well spent.

and this


Are there any patterns or best practices for this issue?
Sb, does your draft version deal about it?
btw: email address is at the end of my posts.


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