Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single click to open menu for tray icon in C#

How do i force a context menu for a tray icon to be shown when it is click rather than just right-clicked.

Ive tried using the MouseClick event, but the eventargs have the mouse position at x0y0.

like image 994
Sam Avatar asked May 07 '09 14:05

Sam


2 Answers

This should do it for you:

private void notifyIcon1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
        }
like image 190
CodeLikeBeaker Avatar answered Sep 30 '22 06:09

CodeLikeBeaker


An alternate method that I have found to work a bit better:

private void notifyIcon1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            System.Reflection.MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            mi.Invoke(notifyIcon1, null);
        }
    }
like image 33
gillonba Avatar answered Sep 30 '22 06:09

gillonba