Can-Ann

I am trying to create a handler for all items and sub items in the menu_click event.

The foreach is given me an error - can anyone explain how to correct.

Code Snippet

#region Handlers

//Recursive method to set the handler for the click event on all menu items

private void SetMenuHandlers(Menu parentMenu, EventHandler handler)

{

foreach (MenuItem mnu in parentMenu)

{

mnu.Click += handler;

SetMenuHandlers(mnu, handler);

}

}

#endregion Handlers

Code Snippet

private void frmMenu_Load(object sender, EventArgs e)

{

//Set the menu handler

SetMenuHandlers(this.Menu, new EventHandler(MenuItem_Click));

}



Re: Windows Forms General Handler for menu click event - foreach error

raghu krishna k

Hi,

check the code in red color

Code Snippet

private void Form1_Load(object sender, System.EventArgs e)

{

SetMenuHandlers(this.Menu, new EventHandler(MenuItem_Click));

}

private void SetMenuHandlers(Menu parentMenu, EventHandler handler)

{

foreach(MenuItem mnu in parentMenu.MenuItems)

{

mnu.Click += handler;

SetMenuHandlers(mnu, handler);

}

}

private void MenuItem_Click(object sender, EventArgs e)

{

this.listBox1.Items.Add(((MenuItem)sender).Text);

}

Thanks






Re: Windows Forms General Handler for menu click event - foreach error

Can-Ann

Thanks for pointing that out - but it only seems to work on the top level of the menu -

ie

Files ---Works

Open -- does not work

Close -- does not work





Re: Windows Forms General Handler for menu click event - foreach error

raghu krishna k

Hi,

Can you post the exact code you wrote for creating and adding menus to the mainmenu

Thanks






Re: Windows Forms General Handler for menu click event - foreach error

Can-Ann

Code was not used - they were added manually.





Re: Windows Forms General Handler for menu click event - foreach error

raghu krishna k

Hi,

i think you are calling the SetMenuHandlers() function before adding the sub menus.

can you check it once.

Thanks






Re: Windows Forms General Handler for menu click event - foreach error

nobugz

Try it like this instead:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void SetMenuHandlers(Menu.MenuItemCollection items, EventHandler handler) {
foreach (MenuItem item in items) {
item.Click += handler;
SetMenuHandlers(item.MenuItems, handler);
}
}
private void Form1_Load(object sender, EventArgs e) {
SetMenuHandlers(this.mainMenu1.MenuItems, MenuItem_Click);
}
private void MenuItem_Click(object sender, EventArgs e) {
Console.WriteLine("Click from {0}", (sender as MenuItem).Text);
}
}






Re: Windows Forms General Handler for menu click event - foreach error

Can-Ann

I am still getting a nullreference exception on the line indicated in red.

The menu items where added via the designer.

Code Snippet

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void SetMenuHandlers(Menu.MenuItemCollection items, EventHandler handler)

{

foreach (MenuItem item in items)

{

item.Click += handler;

SetMenuHandlers(item.MenuItems, handler);

}

}

private void Form1_Load(object sender, EventArgs e)

{

SetMenuHandlers(this.Menu.MenuItems, MenuItem_Click);

}

private void MenuItem_Click(object sender, EventArgs e)

{

Console.WriteLine("Click from {0}", (sender as MenuItem).Text);

}

}

}





Re: Windows Forms General Handler for menu click event - foreach error

nobugz

Odd problem, your form's Menu property isn't assigned. That's normally done by the designer. Instead, try using an explicit reference to your MainMenu component. It was "this.mainMenu1" in my example. Also: check if the menu's GenerateMember property is set to True.





Re: Windows Forms General Handler for menu click event - foreach error

Can-Ann

This is a windows form with a menustrip component named mymenustrip.

in the line :

SetMenuHandlers(this.Menu.MenuItems , MenuItem_Click);

this.Menu is the only item that has .MenuItems.

There have been no changes to the form - it is just to test the process.





Re: Windows Forms General Handler for menu click event - foreach error

nobugz

Now I'm confused. You originally used the Menu class, that's the base class used for the .NET 1.1 MainMenu component. Are you actually using the .NET 2.0 MenuStrip component





Re: Windows Forms General Handler for menu click event - foreach error

Can-Ann

Sorry - it is the menustrip component.





Re: Windows Forms General Handler for menu click event - foreach error

nobugz

Well, that explains your problems. Try this code instead:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void SetMenuHandlers(ToolStripItemCollection items, EventHandler handler) {
foreach (ToolStripMenuItem item in items) {
if (item.DropDownItems.Count == 0) item.Click += handler;
else SetMenuHandlers(item.DropDownItems, handler);
}
}
private void MenuItem_Click(object sender, EventArgs e) {
ToolStripMenuItem item = sender as ToolStripMenuItem;
Console.WriteLine("Click from {0}", item.Text);
}
private void Form1_Load(object sender, EventArgs e) {
SetMenuHandlers(this.menuStrip1.Items, MenuItem_Click);
}
}

I assumed you only wanted a Click event for items without sub-menus. Be sure *not* to use Form.Menu but the instance of the MenuStrip (this.menuStrip1 in my case).

Generally, using one Click event for all menu items is pretty unusual and can make the Click event implementation pretty awkward. As an alternative, consider writing a click event for each individual menu item. Just double-click the item in the designer.





Re: Windows Forms General Handler for menu click event - foreach error

Can-Ann

Thanks nobugz - it worked like a charm and sorry for the misleading info.

If you dont use the toolstrip to create menu's - how do you get them on the form to use your earlier handler

( As you can see I am a newbie).





Re: Windows Forms General Handler for menu click event - foreach error

nobugz

I don't understand your question, please rephrase it.