Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click event on a menu item with submenu items in C#

Status
Not open for further replies.

polocar

Programmer
Sep 20, 2004
89
IT
Hello,
writing my C# program I have noticed that, when I click on a menu item that has some submenu items, the click event isn't generated (I have written a procedure to handle the menu item click event but, even if I have put a breakpoint at the beginning of the handling procedure code, when I click the program doesn't stop).

Is it normal? Is there a way to catch the click event of a menu item with sub menu items and execute the relative handling procedure?

Thank you very much

P.S.: Here I have attached a code snippet that highlights the problem: when you click on the mi1 menu item, the messagebox "You have clicked on MenuItem1" doesn't appear...

Code:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

class TestMenuItem : Form
{
	private MenuItem miFile, mi1, mi1_1, mi1_2;


	public static void Main()
	{
		Application.Run(new TestMenuItem());
	}


	public TestMenuItem()
	{
		miFile = new MenuItem("&File");

		mi1 = new MenuItem("&MenuItem1");
		mi1.Click += new EventHandler(mi1_Click);


		mi1_1 = new MenuItem("M&enuItem1_1");
		mi1_1.Click += new EventHandler(mi1_1_Click);

		mi1_2 = new MenuItem("Me&nuItem1_2");
		mi1_2.Click += new EventHandler(mi1_2_Click);

		Menu = new MainMenu();

		Menu.MenuItems.AddRange(new MenuItem[] {miFile});
		miFile.MenuItems.AddRange(new MenuItem[] {mi1});

		mi1.MenuItems.AddRange(new MenuItem[] {mi1_1, mi1_2});
	}

	private void mi1_Click(object sender, EventArgs e)
	{
		MessageBox.Show("You have clicked on MenuItem1");
	}

	private void mi1_1_Click(object sender, EventArgs e)
	{
		MessageBox.Show("You have clicked on MenuItem1_1");
	}

	private void mi1_2_Click(object sender, EventArgs e)
	{
		MessageBox.Show("You have clicked on MenuItem1_2");
	}
}
 
That is not just C#, it s Nwet FrameWork.
MSDN - MenuItem Click or Select
"Note If the MenuItems property for the MenuItem contains any items, this event is not raised. This event is not raised for parent menu items."

It puts a crimp in plans to Enable/Disable menu items "on the fly".

Compare Code
 
BUT, I forgot that I used the PopUp event for this.
"The Popup event enables you to perform tasks before a menu is displayed. For example, you can create an event handler for this event to display or hide menu items based on the state of your code."



Compare Code
 
Ok, now I have found the note you told me... Thanks John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top