1
votes

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 ??

1

1 Answers

3
votes

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.