gpt4 book ai didi

c# - 上下文菜单项的点击不触发

转载 作者:太空宇宙 更新时间:2023-11-03 23:29:18 24 4
gpt4 key购买 nike

我试图在用户单击上下文菜单中的 MenuItem 时显示消息框警报。这是上下文菜单代码:

    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();
}

点击事件处理程序如下:

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

但是点击事件永远不会被触发。有没有人知道可能出了什么问题??

最佳答案

您的 MenuItem 有您添加的后代:

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

如果你勾选the documentation for the Click event它明确指出(粗体是我的):

If the MenuItems property for the MenuItem contains any items, this event is not raised. This event is not raised for parent menu items.

关于c# - 上下文菜单项的点击不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32842125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com