简体   繁体   English

上下文菜单项的单击不触发

[英]Context Menu Item's click not firing

I'm trying to show a message box alert when the user clicks on a MenuItem inside a context menu. 当用户单击上下文菜单内的MenuItem时,我试图显示消息框警报。 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 : 这是click事件处理程序:

    void mni_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Back Log Event Handler");
    }

But the click event is never fired. 但是click事件永远不会触发。 Does any have any idea what might be wrong ?? 有谁知道可能是什么错误吗?

Your MenuItem has descendants, that you added on: 您的MenuItem具有后代,并在其上添加:

mni.MenuItems.Add("Backlog Task (1)");

If you check the documentation for the Click event it clearly states (bold is mine): 如果您查看Click事件的文档,则会清楚指出(粗体是我的):

If the MenuItems property for the MenuItem contains any items, this event is not raised . 如果MenuItem的MenuItems属性包含任何项目,则不会引发此事件 This event is not raised for parent menu items. 父菜单项不会引发此事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM