I'm new to C#. I want to write a program to display a simple notification. After googling and seeing few stackoverflow answers, I finally wrote a program. But its not working.
Code:
public class SysTrayApp : Form
{
[STAThread]
public static void Main()
{
Application.Run(new SysTrayApp());
}
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public SysTrayApp()
{
trayMenu = new ContextMenu(); // Create a simple tray menu with only one item.
trayMenu.MenuItems.Add("Exit", OnExit);
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon("C:\\Users\\Name\\Desktop\\test.ico", 40, 40); //origin, width, height
trayIcon.ContextMenu = trayMenu;
trayIcon.BalloonTipTitle = "MyTitle";
trayIcon.BalloonTipText = "This is sample message ";
trayIcon.ShowBalloonTip(30000);
trayIcon.Visible = true;
}
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
// Release the icon resource.
trayIcon.Dispose();
}
base.Dispose(isDisposing);
}
}
It is displaying Icon in taskbar, but not showing notification. My requirement is same as this question.