ToolStripButton“重置外观” /“火假事件”

发布于 11-25 23:08 字数 455 浏览 6 评论 0原文

我有一个执行操作的 ToolStripButton。用户单击该按钮,该按钮将被禁用,以防止该操作被执行两次。操作完成后,按钮将重新启用。一切都很完美......除了:

因为按钮被禁用,所以它不会触发“MouseLeave”事件,因此按钮的外观不会更新。为了绝对清楚,当鼠标进入 ToolStripButton 时,该按钮以橙色突出显示(默认情况下),周围有一个黑框。当我重新启用该按钮时,该突出显示不会被删除。此时,鼠标光标早已脱离控件。将鼠标悬停在按钮上自然会通过重绘来修复按钮。

我想做的是 ToolStripButton 上的某种方法“重置”其外观。这样的方法甚至可能存在于 ToolStrip 上,但尽管进行了搜索,但我一直无法找到类似的方法。

作为替代方案,我可以直接在按钮上触发“鼠标离开”事件。据我所知,在 C# .NET 中没有办法轻松做到这一点。

此时的任何建议将不胜感激,我自然不想撕毁我的应用程序并更换工具条。

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 button 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽手叙旧2024-12-02 23:08:25

更新:
我重现了你的问题,试图弄清楚!

除了在点击事件中重置样式之外,我没有找到更好的方法

    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);
    }

希望这有帮助!


您尝试过 Control.Invalidate() 吗?

来自 MSDN:使控件的整个表面无效并导致控件被重绘。

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.

半世晨晓2024-12-02 23:08:25

我也有同样的问题。我通过隐藏 ToolStripButton 然后在任务完成后使用 Visible 属性显示它来“修复”它。

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.

別甾虛僞2024-12-02 23:08:25

在禁用 ToolStrip 或 ToolStripItem 之前:

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

您也可以隐藏和显示整个 ToolStrip,但这可能会影响表单中的其他控件(即,如果您有一些停靠的 DataGridView,它将被重绘)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文