PC Review


Reply
Thread Tools Rate Thread

How to dispose dynamic menu items???

 
 
andylotus@gmail.com
Guest
Posts: n/a
 
      9th Jan 2007
Hi People

I have a WinForm program. The UI contains a ToolStripDropdownButton,
which is associate a list of menu items of ToolStripMenuItem, created
dynamically. Upon different scenarios of user operations, the
DropdowButton need to clean up all its menuitems, and create new ones.

Here the codes:
////////////////////
Void CreateForEmail()
{
....
button.DropDownItems.Clear();
....
button.DropDownItems.Add("To:", null, HandleOutlookToClick);
button.DropDownItems.Add("CC:", null, HandleOutlookCCClick);
}

Void CreateForPhone()
{
....
button.DropDownItems.Clear();
....
button.DropDownItems.Add("Phone", null, HandlePhoneClick);

}

Void CreateForNone()
{
button.DropDownItems.Clear();
}
////////////////////////
So far so good, just work as I expect.
Later on, I found out that " button.DropDownItems.Clear();" does not
dispose the menu items at all, when I decided to add shortcutkeys to
menu items. The above codes were changed in order to have shortCutKeys:
////////////////////
Void CreateForEmail()
{
....
button.DropDownItems.Clear();
....
button.DropDownItems.Add("To:", null, HandleOutlookToClick)
..ShortcutKeys = Keys.F9;;
button.DropDownItems.Add("CC:", null, HandleOutlookCCClick);
}

Void CreateForPhone()
{
....
button.DropDownItems.Clear();
....
button.DropDownItems.Add("Phone", null, HandlePhoneClick) .ShortcutKeys
= Keys.F9;;

}

Void CreateForNone()
{
button.DropDownItems.Clear();
}
////////////////////////

After CreateForNone is called, and I press F9, either
HandleOutlookToClick or HandlePhoneClick will be called, depending
which one was last called. Obviously, those menu items are still in the
program accepting keyboard shortcut, though they are not invisible.

So I replace "button.DropDownItems.Clear();" with the following
////////////////
private void ClearButtonDropdown()
{
for (int i = Button.DropDownItems.Count - 1; i >= 0; i--)
{
ToolStripMenuItem m = Button.DropDownItems[i] as
ToolStripMenuItem;
Button.DropDownItems.Remove(m);
m.Dispose();
}
Button.DropDownItems.Clear();
// even GC.Collect() will not have effect.
}
///////////////
However, strange things remain the same.
Obviously someone is still holding references to those menu items,
stopping GC from disposing those items.

Can you point out how to dispose these menu items?

Cheers


Andy

 
Reply With Quote
 
 
 
 
John J. Hughes II
Guest
Posts: n/a
 
      10th Jan 2007
Just a guess, have you tried clearing the shortcut key before deleting the
item?

Regard,
John

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi People
>
> I have a WinForm program. The UI contains a ToolStripDropdownButton,
> which is associate a list of menu items of ToolStripMenuItem, created
> dynamically. Upon different scenarios of user operations, the
> DropdowButton need to clean up all its menuitems, and create new ones.
>
> Here the codes:
> ////////////////////
> Void CreateForEmail()
> {
> ...
> button.DropDownItems.Clear();
> ...
> button.DropDownItems.Add("To:", null, HandleOutlookToClick);
> button.DropDownItems.Add("CC:", null, HandleOutlookCCClick);
> }
>
> Void CreateForPhone()
> {
> ...
> button.DropDownItems.Clear();
> ...
> button.DropDownItems.Add("Phone", null, HandlePhoneClick);
>
> }
>
> Void CreateForNone()
> {
> button.DropDownItems.Clear();
> }
> ////////////////////////
> So far so good, just work as I expect.
> Later on, I found out that " button.DropDownItems.Clear();" does not
> dispose the menu items at all, when I decided to add shortcutkeys to
> menu items. The above codes were changed in order to have shortCutKeys:
> ////////////////////
> Void CreateForEmail()
> {
> ...
> button.DropDownItems.Clear();
> ...
> button.DropDownItems.Add("To:", null, HandleOutlookToClick)
> .ShortcutKeys = Keys.F9;;
> button.DropDownItems.Add("CC:", null, HandleOutlookCCClick);
> }
>
> Void CreateForPhone()
> {
> ...
> button.DropDownItems.Clear();
> ...
> button.DropDownItems.Add("Phone", null, HandlePhoneClick) .ShortcutKeys
> = Keys.F9;;
>
> }
>
> Void CreateForNone()
> {
> button.DropDownItems.Clear();
> }
> ////////////////////////
>
> After CreateForNone is called, and I press F9, either
> HandleOutlookToClick or HandlePhoneClick will be called, depending
> which one was last called. Obviously, those menu items are still in the
> program accepting keyboard shortcut, though they are not invisible.
>
> So I replace "button.DropDownItems.Clear();" with the following
> ////////////////
> private void ClearButtonDropdown()
> {
> for (int i = Button.DropDownItems.Count - 1; i >= 0; i--)
> {
> ToolStripMenuItem m = Button.DropDownItems[i] as
> ToolStripMenuItem;
> Button.DropDownItems.Remove(m);
> m.Dispose();
> }
> Button.DropDownItems.Clear();
> // even GC.Collect() will not have effect.
> }
> ///////////////
> However, strange things remain the same.
> Obviously someone is still holding references to those menu items,
> stopping GC from disposing those items.
>
> Can you point out how to dispose these menu items?
>
> Cheers
>
>
> Andy
>



 
Reply With Quote
 
Andy Lotus
Guest
Posts: n/a
 
      14th Jan 2007

John J. Hughes II wrote:
> Just a guess, have you tried clearing the shortcut key before deleting the
> item?
>
> Regard,
> John
>


Thanks John.

Yes, it work fine now. Just before I clear the menu items, I check
every items and assign ShortcutKeys with ShortcutKeys.None.

I think (guess) the shortcuts work this way:
1. Application traps key inputs for hot keys at application level
2. Form traps key inputs for shortcuts at window level.
3. When assigning a shortcut to a menu item, the item is registered
with some kind of Shortcut Manager somewhere.
4. When user presses a shortcut, the shortcut manager will be aware of
it, and trigger the menu item associated with the shortcut.
5. When I call dispose upon the menu item, the Shortcut Manager still
have reference to the item, so GAC can do nothing about it.
6. After I assigned None to the item, apparently this will unregister
the item from the manager.

In addition, I also check the memory usage through Process Manager.
After I created those dynamic menu items, say a few dozens times per
minutes, the memory usage kept stable, obviously those items got
disposed and reused normally. I felt happy every since.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET Menu Items - Dynamic Items Disapearing Microsoft Newsserver Microsoft ASP .NET 1 22nd Feb 2008 04:18 PM
???Dispose dynamic menu items Andy Lotus Microsoft Dot NET Framework Forms 1 10th Jan 2007 09:12 AM
Dynamic Context Menu Items www.dir@gmail.com Microsoft Dot NET Framework Forms 5 17th Jul 2005 09:37 AM
Generic menu event for a menu with dynamic items Mac via DotNetMonster.com Microsoft VB .NET 2 26th May 2005 01:37 PM
Creating Dynamic Menu Items Stuart Ferguson Microsoft Dot NET 1 4th Jul 2004 12:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:55 PM.