Search code examples
c#winformsmenuitemmousehover

C# WinForms trayapp MenuItem mouse hover detection


I have a fairly simple question, but I'm failing to find the solution. I have an application that resides in the task tray. When a user right clicks the tray icon, the program displays a menu of MenuItems. I would like to execute code when some of my MenuItems are mouse hovered over.

Is this possible?

Can you send me in the right direction?

I am using NotifyIcon

        trayMenu = new ContextMenu();
        trayMenu.MenuItems.Add("Exit", OnExit);

        trayIcon = new NotifyIcon();
        trayIcon.Text = "blah";

        trayIcon.Icon = new Icon("favicon.ico", 40, 40);
        trayIcon.ContextMenu = trayMenu;
        trayIcon.Visible = true;

Solution

  • You'll have to use MouseHover or MouseEnter and MouseLeave events of each menuitem.

    Update: Yep, NotifyIcon controls have a property named ContextMenuStrip. You'll have to create the ContextMenuStrip control to display the menu. It contains items of ToolStripMenuItems type. I tried to create a simple prototype - MouseHover works just fine.