I'm trying to show a message box alert when the user clicks on a MenuItem inside a context menu. Here is the context menu code:
public Form1()
{
MenuItem mni = new MenuItem();
mni.Text = "BackLog Task";
mni.MenuItems.Add("Backlog Task (1)");
mni.Click += new EventHandler(this.mni_Click);
contextMenu1.MenuItems.Add(mni);
notifyIcon1.Visible = true;
notifyIcon1.Icon = new System.Drawing.Icon
(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal)
+ @"\icon.ico");
notifyIcon1.Text = "Right-click me!";
notifyIcon1.ContextMenu = contextMenu1;
InitializeComponent();
}
Here is the click event handler :
void mni_Click(object sender, EventArgs e)
{
MessageBox.Show("Back Log Event Handler");
}
But the click event is never fired. Does any have any idea what might be wrong ??
Your MenuItem
has descendants, that you added on:
mni.MenuItems.Add("Backlog Task (1)");
If you check the documentation for the Click event it clearly states (bold is mine):
If the MenuItems property for the MenuItem contains any items, this event is not raised . This event is not raised for parent menu items.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.