Search code examples
c#notifyicon

How can I make a menu for notifyicon?


So.. i've googled around and everywhere i've seen different ways of creating this..

But so far, i haven't managed to make a single working menu.

So i wanted to ask, how does one create a notifyIcon menu?.. (prefered explained in details, as i'm rather new to this)

which way would be best and which should i use.. (so far people seemed to like contextmenu overally, but all i can find is contextmenustrip, not sure if it's the same.)

Currently i got a form, set to visible = false, windowstate minimized, showintaskbar = false.

that's about all it is for now. i wanted to have the menu before going wider.

Thank you for your time and effort for this (not sure if it's formulated properly)

EDIT: i've seemed to manage to make a menu, but how would i make it "appear" on my notify icon, it's a ContextMenu o_o


Solution

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace TrayTest.events
    {
        public partial class TrayMenu : Form
        {
            public TrayMenu()
            {
                InitializeComponent();
                TrayMenuContext();
            }
    
            private void TrayMenuContext()
            {
                this.notify_icon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
                this.notify_icon.ContextMenuStrip.Items.Add("Test1", null, this.MenuTest1_Click);
                this.notify_icon.ContextMenuStrip.Items.Add("Test2", null, this.MenuTest2_Click);
                this.notify_icon.ContextMenuStrip.Items.Add("Exit", null, this.MenuExit_Click);
            }
    
            void MenuTest1_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            void MenuTest2_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            void MenuExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
        }
    }
    

    This worked fine for me. So i'll just leave it here, for other to take a peak at it.. (this is my Form1, just made 1 with a different name, and it's inside a folder named events (kinda why it has that .events))