how do i open a menu to exit the application when i right click on notifyicon in the systemtray? thanks.

Dani AI

Generated

's approach is the right starting point. A couple of practical, production-minded tips will make the tray menu and app exit behave reliably across machines and Windows versions.

Keep the NotifyIcon cleaned up on exit — otherwise you can get the infamous "ghost" icon left in the tray. Hide and dispose the NotifyIcon before ending the process; attach the same cleanup to your FormClosing or ApplicationExit handlers so it always runs:

' call from your "Exit" menu item
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    NotifyIcon1.Visible = False
    NotifyIcon1.Dispose()
    Application.Exit()
End Sub

' safe cleanup on form close / app exit
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    If NotifyIcon1 IsNot Nothing Then
        NotifyIcon1.Visible = False
        NotifyIcon1.Dispose()
    End If
End Sub

Other useful notes not shown earlier: ensure NotifyIcon.Icon is set to a valid .ico and NotifyIcon.Visible = True (otherwise the tray behavior can be inconsistent). If you need to show the menu programmatically at the pointer, use ContextMenuStrip.Show(Cursor.Position) so it always appears under the cursor. Use the ContextMenuStrip.Opening event to refresh Enable/Visible state of items right before the menu appears. Finally, keep all NotifyIcon work on the UI thread (creating it on a background thread can cause odd behavior).

These small additions complement ’s answer and prevent the common pitfalls that ran into when wiring an exit option into a tray icon.

Recommended Answers

All 2 Replies

See if this helps.
1 NotifyIcon, 1 ContextMenuStrip

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NotifyIcon1.ContextMenuStrip = ContextMenuStrip1 '// attach ContextMenu to NotifyIcon.
    End Sub

    Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        If e.Button = MouseButtons.Right Then NotifyIcon1.ContextMenuStrip.Show() '// Show ContextMenu on Right Mouse click.
    End Sub
End Class

Thanks man, that helped alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.