How can I make a menu for notifyicon?

12.2k views Asked by At

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

3

There are 3 answers

0
Komari Kamikita On BEST ANSWER
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))

2
Idle_Mind On

"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"

I believe you can only assign a ContextMenuStrip to the NotifyIcon using the IDE. For a ContextMenu, you'd have to wire it up via code. Double click your Form to get the Load() event, and wire it up in there:

    private void Form1_Load(object sender, EventArgs e)
    {
        notifyIcon1.ContextMenu = contextMenu1;
    }
0
Nico On
notifyIcon1->ContextMenu = gcnew System::Windows::Forms::ContextMenu();

System::Windows::Forms::MenuItem^ nI_Open_Item   = gcnew System::Windows::Forms::MenuItem("Open");
System::Windows::Forms::MenuItem^ nII_Close_item = gcnew System::Windows::Forms::MenuItem("Close");

notifyIcon1->ContextMenuStrip->Items->Add(status_Item);
notifyIcon1->ContextMenu->MenuItems->Add(nI_Open_Item);