views:

51

answers:

1

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!

A: 

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