Search code examples
c#keyboard-shortcutsmessagebox

C# MessageBox link opens several times by F1 shortcut


I have a Help menu in my C# application, which contains a "Filtering" item. This item displays a default MessageBox with an explanation, and the buttons are OK and Help which opens a link in MSDN.

string link = "http:...";
MessageBox.Show("explanation...", "Filtering", MessageBoxButtons.OK,
    MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0, link);

Opening the message through the item and opening the link through the 'Help' button works OK.

The problem:
I added a shortcut key for "Filtering" (F1). Now when I press F1 in the application, not only it opens the message box AND the link, it opens the link 2 or 3 times.
The behavior is such:

  • Clicking the item still works fine.
  • Pressing F1 when first opened the application - the link opens automatically 3 times (3 tabs of the same page).
  • Pressing F1 next time (after closing the message box) - the link opens automatically 2 times.

The problem is about using F1 because it's also the key to the Help button in the box, but it still doesn't explain why the key event occurs also in the box that just opened, and why the link opens 2 or 3 times. What I checked:

  • The message box opens one time.
  • The code that creates and opens the message box runs one time - when I press down the F1.
  • Changing the shortcut key to F10 or something else prevents the problem, but I want to use the F1 as the standard Help shortcut.

Can anyone think of a way to solve this? I'd have no problem even if the link opened one time only.


Solution

  • Try this:

    private bool isClicked = false, secondTime = false;
    
    private void FilteringToolStripMenuItem_Click(object sender, EventArgs e)
    {    
        isClicked = true;
        string link = "http:...";
        MessageBox.Show("explanation...", "Filtering", MessageBoxButtons.OK,
        MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0, link);
        isClicked = false; 
    }
    
    private void ShowMsgBox()
    {
        secondTime = true;
        string link = "http:...";
        MessageBox.Show("explanation...", "Filtering", MessageBoxButtons.OK,
        MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0, link);
        secondTime = false;
    }
    
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x53) //WM_HELP message
        {
            if (isClicked == false)
            {
                if (secondTime == false)
                {
                    ShowMsgBox();
                }
            }
        }
    
        base.WndProc(ref m);
    }
    

    Remove the line:

    this.FilteringToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F1;
    

    When you press the F1 key, FilteringToolStripMenuItem_Click is called, the link is opened and then a WM_HELP message is generated and another link is opened. With the above code we check if WM_HELP message is generated when no message box is opened (secondTime = false and isClicked = false), and when a message box is opened through the ToolStripMenuItem (isClicked = true)