Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2007

Tip: Looking for answers? Try searching our database.

???Dispose dynamic menu items

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy Lotus - 10 Jan 2007 03:40 GMT
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
Jeff Gaines - 10 Jan 2007 09:12 GMT
On 10/01/2007 in message

>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?

This is a function to remove items from a List - it is in C# but the
principle is the same:

public void Remove(ContainerListViewItem item)
{
    item.MouseDown -= new MouseEventHandler(OnMouseDown);
    List.Remove(item);
}

As you can see it removes the event handler so allowing the object to be
disposed. I haven't done BASIC for years so I don't know if you have the
'-=' operator though?

Signature

Jeff Gaines


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.