Dynamically creating a menu

  • Thread starter Thread starter Asheesh
  • Start date Start date
A

Asheesh

Hi All,

I'd appreciate if someone could provide me a better way of doing this.

I need to show a different menubar for a particular screen. The interesting
part is that the U.I of the Form is the same but when the app runs in a
different mode, the same screen is presented with a different menubar.

For this I'm dynamically creating the Menubar based on the Mode in which my
application is running.

My menubar is something like this Mode 1:

File Mode Lang Find Rep

My menubar for Mode 2 is

File Mode Opt Lang Find Hist

Now in case of Opt I've a lot of other sub-menus as well.

What I've done is declared all my menuitems in my class level with a private
scope. Now when the user selects a particular mode, I dynamically create
all the menuitems/submenuitems and assign the mainmenu to the Form's Menu.

I do this whenever the user changes the Mode. This approach works fine, but
everytime I've to clear the previous menuitems, and then create the new
menubar.

It's mnuMain.menuitems.clear();
CreateNewModeMenu();

But taking this approach leads to so many menuitem objects created everytime
I change the mode of my application. In fact when I look at the available
memory, it reduces to quite an extent everytime I perform the mode
operation.

I want to know that are the menuitems garbage collected when I call the
mnuMain.menuitems.clear() method?

Is there another way where I can prevent creation and deletion of so many
menuitems again and again so that only once do I create a menubar for each
mode, and just replace the menu for the form?

Please help...
Regards,
Asheesh
 
Asheesh,

How about just creating 2 different menu structures and swapping them in and
out depending on the circumstances?
 
Hi Ginny,

Thanks a lot for your reply. I can create 2 different menu structures and
then swap them according to the circumstances, but loading 2 mainmenu
structures in the memory for the entire duration of the application would be
too costly or wouldn't it be?
This solution is definitely better than the creating a dynamic menu
structure everytime, but I would like to know your thoughts about the
presence of 2 menu structures at the same time throughout the duration of
the application.

Another thing, Ginny, When I clear the menuitems using
Menu.menuItems.clear() method, is the memory for the respective menuitems
released, and are they garbage collected or is it simply clearing away the
menuitem collection?

Please help!
Thanks a lot once again.
Regards,
Asheesh
 
Asheesh,

I doubt that two menu structures in memory at the same time will make a big
difference. Why not just try it! Many people are happy with the strategy of
keeping several forms in memory and just switching them in and out as
needed, so this is really the same thing.

Memory is released by .Net when it needs more memory and not as soon as
*you* are finished with it.
 
Back
Top