0

Adding the event handler click event was pretty straightforward when I followed the documentation at Microsofts web pages. Unfortunately there was no example including the MouseDown event.

I've tried quite a few combinations but I must be using the wrong syntax or wrong declarations.

This works fine:

    notifyIcon.Click += new System.EventHandler(NotifyIcon_Click);
    System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
    System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem();
    
    contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItemExit });

    menuItemExit.Index = 0;
    menuItemExit.Text = "E&xit";
    menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
    notifyIcon.ContextMenu = contextMenu;
}

private void NotifyIcon_Click(object Sender, EventArgs e)
{
    this.Visibility = Visibility.Visible;
    this.Activate();
}

But this does not:

notifyIcon.MouseDown += new System.EventHandler(NotifyIcon_MouseDown);
    
    System.Windows.Forms.ContextMenu contextMenu = new 
    System.Windows.Forms.ContextMenu();

    System.Windows.Forms.MenuItem menuItemExit = new 
    System.Windows.Forms.MenuItem();
    
contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]{menuItemExit});

    menuItemExit.Index = 0;
    menuItemExit.Text = "E&xit";
    menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
    notifyIcon.ContextMenu = contextMenu;
}

private void NotifyIcon_MouseDown(object Sender, EventArgs e)
{
    this.Visibility = Visibility.Visible;
    this.Activate();
}

enter image description here

What I'm trying to achieve here is for the context menu to open on a right click and the application itself on the left click of the notification icon. I was hoping that on the MouseDown event I would be able to detect whether the left or right mouse button is down.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mogash
  • 130
  • 1
  • 7
  • 1
    Please, read an error message carefully. :) Then, read this: [NotifyIcon.MouseDown Event](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.notifyicon.mousedown?view=windowsdesktop-6.0) – Maciej Los Nov 23 '21 at 12:14

2 Answers2

0

I suggest you to add event inside page_load event to trigger if its ASP.Net web application.

protected void Page_Load(object sender, EventArgs e) { }.

Based on how you have added that event looks correct.

If this is forms application then you need to handle it inside constructor with syntax as below:

this.nitifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseDown);

Rohit Tatiya
  • 361
  • 2
  • 7
0

I see I got a few responses already. While waiting I suddenly stumbled upon a solution to the problems I've been facing.

Source: Invoke NotifyIcon's Context Menu

   private void SetNotifyIconSettings()
            {
    
                notifyIcon.Text = "My Application";
                notifyIcon.Icon = new System.Drawing.Icon("now-agent-icon.ico");
                notifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseDown) ;
                
                System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem();
               
                contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItemExit });
                menuItemExit.Index = 0;
                menuItemExit.Text = "E&xit";
                menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
                notifyIcon.ContextMenu = contextMenu;
    
            }
    
            private void NotifyIcon_MouseDown(object Sender, System.Windows.Forms.MouseEventArgs e)
            {
                if(e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    this.Visibility = Visibility.Visible;
                    this.Activate();
                }
    
                if(e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    MethodInfo mi = typeof(System.Windows.Forms.NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
                    mi.Invoke(notifyIcon, null);
    
                }
    
            }
            private void menuItemExit_Click(object Sender, EventArgs e)
            {
                
                CloseApplication();
            }
Mogash
  • 130
  • 1
  • 7