How to add event handler for MouseDown event?

919 Views Asked by At

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.

2

There are 2 best solutions below

0
Rohit Tatiya On BEST ANSWER

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

0
Mogash On

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