Dynamic Context Menus

E

Eddie de Bear

Hi,

A project I am working on has a requirement for dynamic
menus. For the most part this works really well.

The menus I'm creating a based on files and directories,
so naturally the menu creation takes some time.

The approach I took was to override the OnSelect method
of the MenuItem class, which had code to populate the
child items.

This worked really well until I had to use the menu as a
context menu.. I found that the context menu does not
refresh properly below the first level menu..

The only way I can get this to work correctly is to use
reflection to call the private UpdateMenuItem method on
the MenuItem class.

Has anybody else had experiance with this??

Is the fact that the MenuItem does not refresh correctly
when in a context menu a bug???

Any feedback would be great..

Cheers
 
M

Mick Doherty

I just took a look at that and it does appear to be a serious BUG.
A workaround, other than your own, is to set the forms ContextMenu property
to the context menu in question. Of course this is not the ideal situation
as your context menu will now popup from every control that does not have
it's own context menu.

As for that slow menu generation, are you adding the MenuItems to your
ContextMenu in a loop? If so, a faster way is to create a MenuItem Array and
set each member of the array to a new menuitem from within the loop. Then
outside the loop use AddRange to add all the menu items at once.
 
E

Eddie de Bear

The reason me menu creation is slow is because the menu
is based on existance of files in the OS as well as
security settings in the database... This retrieval takes
time.. Not the actual menuitem creation itself..

I was actually hoping some of the MS guys could possibly
take a look at this problem, becuase I have spent several
days on this, and have come to the conclusion that it is
a bug...
 
M

Mick Doherty

Well this was really bugging me and I just couldn't let it go.
After many unsuccessful overcomplicated attempts I finally came up with a
very simple solution.

Remove the MenuItem that you wish to add items to, add the items then re-add
the Menuitem to the ContextMenu.

VB.Net Example

Dim DynamicParentIndex As Integer = DynamicParent.Index
MyContextMenu.MenuItems.Remove(DynamicParent)
DynamicParent.MenuItems.Clear 'Optional
DynamicParent.MenuItems.AddRange(DynamicMenuItemsArray)
MyContextMenu.MenuItems.Add(DynamicParentIndex, DynamicParent)
 

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

Similar Threads

Build dynamic context menus for trees 2
Context Menus 1
Menus and MasterPages 6
explorer context menus 1
Identifying menus 1
Sharing of Context Menus 5
context menu font 3
Creating Dynamic Menu Items 1

Top