Advanced 12 min read Control #14

NotifyIcon Control

NotifyIcon adds an icon to the system tray (notification area) and allows you to show balloon tips and context menus.

What is a NotifyIcon?

NotifyIcon is a non-visual component that displays an icon in the system tray. It's perfect for background applications that need to show notifications or provide quick access from the taskbar.

Code Example

// Setup NotifyIcon
notifyIcon1.Icon = SystemIcons.Information;
notifyIcon1.Text = "My Application";
notifyIcon1.Visible = true;

// Show balloon tip
notifyIcon1.ShowBalloonTip(3000, "Notification", 
    "Hello! This is a balloon tip.", 
    ToolTipIcon.Info);

// Handle click on NotifyIcon
private void notifyIcon1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Notification clicked!");
}

// Add ContextMenuStrip
// In designer, set ContextMenuStrip property
// Add items: Open, Settings, Exit

private void openMenuItem_Click(object sender, EventArgs e)
{
    // Show your main form
    ShowForm();
}

private void exitMenuItem_Click(object sender, EventArgs e)
{
    notifyIcon1.Visible = false;
    Application.Exit();
}

Exercise

Task: Create a background timer app with NotifyIcon.

  1. Add a NotifyIcon with an icon and tooltip.
  2. Add a ContextMenuStrip with: Show, Hide, Exit.
  3. Add a Timer that shows a balloon tip every 30 seconds.
  4. When the user clicks the notify icon, show/hide the main form.
  5. When the user closes the form, hide it and keep the app running.
Key Takeaway

NotifyIcon is essential for background applications. Use it with ContextMenuStrip to provide quick access to features from the system tray.