Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add ContextMenu to the system tray icon programmatically?

I want to programmatically add a context menu to my tray icon, so that when I right-click on the tray icon, it should show me the menu.How should I write the right-click event handler for my tray icon?

I have tried the below:

private void Icon_MouseRightClick(object sender, MouseButtonEventArgs e)
{
 if (e.Button == System.Windows.Forms.MouseButtons.Left)  // shows error ate button
 {
   return;
 }
 if (e.Button == System.Windows.Forms.MouseButtons.Right)
 {
   // code for adding context menu
 }
}

Declared Eventhandler as,

NotifyIcon.MouseRightClick += new MouseButtonEventHandler(NotifyIcon_MouseRightClick);
like image 204
user2622971 Avatar asked Jul 30 '13 11:07

user2622971


People also ask

How to add icon to Context menu in c#?

If you're talking about ContextMenuStrip control, you can do that either it in the designer, by clicking on the item and selecting "Set image...", or programmatically by changing the Image property of the ToolStripMenuItem .

What is a Systray application?

The system tray (or "systray") is a section of the taskbars in the Microsoft Windows operating system (OS) user interface that provides easy access icons to the user's most commonly used apps and displays the clock.


1 Answers

Context menu on right-click is automatic, no need to handle it. Just build your menu and assign it to NotifyIcon.ContextMenu.

like image 173
piedar Avatar answered Oct 03 '22 18:10

piedar