开发者

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:

Because the b开发者_如何学JAVAutton is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.

What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.

As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.

Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.


Update: I reproduced your problem, trying figuring out!

I didn't get a better way other than reset the style in the click event

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
        toolStripButton1.Enabled = false;
    }

    private void toolStripButton1_MouseEnter(object sender, EventArgs e)
    {
        toolStripButton1.BackColor = Color.Red;
    }

    private void toolStripButton1_MouseLeave(object sender, EventArgs e)
    {
        toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
    }

Hope this helps!


Have you tried Control.Invalidate()?

from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.


I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.


Before disabling ToolStrip or ToolStripItem:

private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
        {
            foreach (ToolStripItem item in toolStrip.Items)
            {
                if (item.Pressed || item.Selected)
                {
                    item.Visible = false;
                    item.Visible = true;
                }
            }
        }

also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜