1

I have a WinForms application containing a ToolStrip with ToolStripButtons. Some of the button actions disable the main form while the button action takes place and re-enable it when finished. This is done to make sure the user does not click on other places while the action takes place, and also shows a WaitCursor but that's not relevant to the issue.

If the user clicks on the button and moves the mouse cursor outside of its bounds while the form is disabled, the button remains highlighted (transparent blue) even when re-enabling the form at a later point. If the mouse enter/leaves the button afterwards it is displayed correctly again.

I could artificially replicate the problem by showing a MessageBox using the following code (the actual action does not show a message box, but opens a new form and populates a grid, but the net effect is the same).

Here is a code snippet to replicate the issue:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        // Disable the form
        Enabled = false; 

        // Some action where the user moved the mouse cursor to a different location
        MessageBox.Show(this, "Message");

        // Re-enable the form
        Enabled= true; 
    }
}
Andres
  • 121
  • 6
  • Did you try adding `this.Refresh();` as the first line in `toolStripButton1_Click`? – ispiro Nov 28 '16 at 19:01
  • I now tried your code and the blue background disappears when i close the MessageBox. – ispiro Nov 28 '16 at 19:30
  • @ispiro Tried with `this.Refresh();` but the issue still occurs. Perhaps you closed the MessageBox with Enter instead of clicking with the mouse? Not sure if it's relevant but I'm using Visual Studio 2013 and .NET Framework 4.5. – Andres Nov 29 '16 at 14:06
  • It works fine both with the mouse and with "Enter". I'm using VS2015 (64 bit Windows 10). I also transformed it to .net 4.5 (from 4.5.2) and it sill works fine. I don't know what might be the difference. Good luck. – ispiro Nov 29 '16 at 15:27
  • ToolStripButton does a pretty good job of making itself look disabled without any help. Not that obvious what you might be doing wrong, but not picking a good bitmap for the button has to be the underlying issue. – Hans Passant Nov 29 '16 at 19:44
  • @HansPassant On this sample I did not add an external image to the button (just used the default one), but I also replicated the same problem changing the DisplayStyle to "Text". Note that if I disable/enable the toolStripButton then it works fine, the problem occurs when disabling the form. – Andres Nov 30 '16 at 14:03

1 Answers1

4

I finally found a solution.

I created this extension method that uses reflection to call the private method "ClearAllSelections" on the parent toolstrip:

    public static void ClearAllSelections(this ToolStrip toolStrip)
    {
        // Call private method using reflection
        MethodInfo method = typeof(ToolStrip).GetMethod("ClearAllSelections", BindingFlags.NonPublic | BindingFlags.Instance);
        method.Invoke(toolStrip, null);
    }

and call it after re-enabling the form:

private void toolStripButton1_Click(object sender, EventArgs e)
{
    // Disable the form
    Enabled = false; 

    // Some action where the user moved the mouse cursor to a different location
    MessageBox.Show(this, "Message");

    // Re-enable the form
    Enabled= true;

    // Hack to clear the button highlight
    toolStrip1.ClearAllSelections();
}
Andres
  • 121
  • 6