Menu how to figured out

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);
}
 
B

Brendan Grant

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);
}


.
 

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

Top