1

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.

Community
  • 1
  • 1
gangadhars
  • 2,584
  • 7
  • 41
  • 68
  • 1
    Did you try to move ShowBalloonTip() after Visible = true? – Adriano Repetti Aug 01 '14 at 10:49
  • 1
    I traied, but no use @AdrianoRepetti – gangadhars Aug 01 '14 at 10:50
  • possible duplicate of [How to show a message with icon in notification are using C#](http://stackoverflow.com/questions/15215716/how-to-show-a-message-with-icon-in-notification-are-using-c-sharp) – Mwigs Aug 01 '14 at 11:09
  • 2
    Sorry @AdrianoRepetti. I tried first. But i donno, on that time it didn't worked. – gangadhars Aug 01 '14 at 11:09
  • in my case it was working until I added `SystemSounds.Beep.Play()` then notification balloon **NEVER** showed up again, regardless of the code. I have tried any sample code on line and none worked again. Restarted VS, created new solution, restarted windows, etc, to no avail. – derloopkat Jul 01 '18 at 11:22

1 Answers1

-1

The problem is that the code that shows the BallonTip resides in the constructor of the form. You must place the ShowBallonTip() call in a method that runs after that the form is 'costructed'.

SpeedJack
  • 139
  • 2
  • 8