2017-07-26 81 views
0

我有一个WinForms应用程序,只能在托盘中启动。点击它,它会打开一个表单。这工作正常。单击NotifyIcon上下文菜单中的ContextMenuItem调用NotifyIcon单击事件

notifyIcon.Click += notifyIcon_Click; 

//Fires on icon click, AND on contextmenuitem click 
    private void notifyIcon_Click(object sender, EventArgs e) 
      { 
       new ActiveIssues(_hubProxy).Show(); 
      } 

我添加了一个上下文菜单,但是当我点击的ContextMenuItem,它首先触发NotifyIcon的单击事件,然后点击的ContextMenuItem事件,开两种形式。

notifyIcon.ContextMenu = GetCrestContextMenu(); 

    private ContextMenu GetCrestContextMenu() 
      { 
       var contextMenu = new ContextMenu(); 
       contextMenu.Name = "CResT Alerts"; 
       contextMenu.MenuItems.Add(GetTextOptionMenuItem()); 
       return contextMenu; 
      } 

      private MenuItem GetTextOptionMenuItem() 
      { 
       var textOptionMenuItem = new MenuItem { Text = _textOptedIn ? "Opt Out of Text Alerts" : "Opt In to Text Alerts" }; 
       textOptionMenuItem.Click += TextOptionMenuItem_Click; 
       return textOptionMenuItem; 
      } 

//Fires on menuitem click, after the NotifyIcon click event is called 
      private void TextOptionMenuItem_Click(object sender, EventArgs e) 
      { 
       if (_textOptedIn) new TextOptOut().Show(); 
       else new TextOptIn().Show(); 
      } 

不知道如何要么没有它火的notifiyicon点击事件,或者告诉点击是上下文菜单上?

+1

不记得行为,没有看到任何明显的错误,但你总是可以看看'sender'来看看被点击的东西。 – Will

+0

是的,我也这么认为,但发件人是该点击的ContextMenuItem,以及该点击的NotifyIcon。原来,这是右键单击以获取导致我的问题的上下文菜单。不过感谢您的帮助! – Mike

回答

1

所以事实证明,右键单击不会被注册,直到上下文菜单被点击后,所以它是右键单击注册并引发了NotifyIcon点击事件。因此,我必须将提供用于点击的EventArgs作为MouseEventArgs,并检查按钮。

private void notifyIcon_Click(object sender, EventArgs e) 
    { 
     if(((MouseEventArgs)e).Button == MouseButtons.Left) new ActiveIssues(_hubProxy).Show(); 
    } 
相关问题