Menu how to figured out

  • Thread starter Thread starter web1110
  • Start date Start date
W

web1110

I got my menu stuff figured out. I created a main menu with one menu item
(menuItem1). When selected, it fills in submenu items and links them to a
click method. On each select, the subitems are cleared out.

I have two more questions

1. When I do the RemoveAt, is the link to the clicker method obliterated?
It seems ok. I don't want to keep adding things into the list.
2. It there an easier way to do the RemoveAt loop, like in one fell swoop?


private void menuItem1_Select(object sender, System.EventArgs e)
{
for(int si=menuItem1.MenuItems.Count-1; si>=0;si--)
{
menuItem1.MenuItems.RemoveAt(si);
}

string[] astrWindow={"111", "222", "333", "444", "555"};
for(int si=0; si<astrWindow.Length;si++)
{
MenuItem filemenu = new MenuItem();
filemenu.Click += new System.EventHandler(this.fmClick);
filemenu.Text = astrWindow[si];;
menuItem1.MenuItems.Add(filemenu);
}
}

private void fmClick(object sender, System.EventArgs e)
{
MessageBox.Show(((MenuItem)sender).Text);
}
 
Yes, when you remove the item from the list, the reference
to the event handler is also removed provided there are no
other references to the menu item you have just removed.

To nuke all of the MenuItems from a MenuItems collection,
simply call the Clear() method on it.

So in your example, you could simply replace:

for(int si=menuItem1.MenuItems.Count-1; si>=0;si--)
{
menuItem1.MenuItems.RemoveAt(si);
}

with:

menuItem1.MenuItems.Clear();

-----Original Message-----
I got my menu stuff figured out. I created a main menu with one menu item
(menuItem1). When selected, it fills in submenu items and links them to a
click method. On each select, the subitems are cleared out.

I have two more questions

1. When I do the RemoveAt, is the link to the clicker method obliterated?
It seems ok. I don't want to keep adding things into the list.
2. It there an easier way to do the RemoveAt loop, like in one fell swoop?


private void menuItem1_Select(object sender, System.EventArgs e)
{
for(int si=menuItem1.MenuItems.Count-1; si>=0;si--)
{
menuItem1.MenuItems.RemoveAt(si);
}

string[] astrWindow=
{"111", "222", "333", "444", "555"};
for(int si=0; si<astrWindow.Length;si++)
{
MenuItem filemenu = new MenuItem();
filemenu.Click += new System.EventHandler (this.fmClick);
filemenu.Text = astrWindow[si];;
menuItem1.MenuItems.Add(filemenu);
}
}

private void fmClick(object sender, System.EventArgs e)
{
MessageBox.Show(((MenuItem)sender).Text);
}


.
 
Back
Top