3

I have a sample C# windows form here. I need to show the notification icon's context menu when it is left-mouse clicked. I have marked where to write the needed codes as below:

private void button1_Click(object sender, EventArgs e)
{
        //Need to show the context menu here
}

Please help!

Nam G VU
  • 33,193
  • 69
  • 233
  • 372

1 Answers1

5

To show the menu when your icon is left-clicked

private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        MethodInfo methodInfo = typeof(NotifyIcon).GetMethod("ShowContextMenu",
            BindingFlags.Instance | BindingFlags.NonPublic);

        methodInfo.Invoke(this.notifyIcon, null);
    }
}

To show the menu when the button in your question is clicked

private void button1_Click(object sender, EventArgs e)
{
    //Need to show the context menu here
    MethodInfo methodInfo = typeof(NotifyIcon).GetMethod("ShowContextMenu",
        BindingFlags.Instance | BindingFlags.NonPublic);
    methodInfo.Invoke(this.notifyIcon, null);
}
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63