如何在禁用的按钮上显示工具提示? [英] How can I show a tooltip on a disabled button?

查看:453
本文介绍了如何在禁用的按钮上显示工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在winform上有一个禁用的按钮,如何在鼠标悬停时显示工具提示,以通知用户为什么禁用了该按钮?

If you have a disabled button on a winform how can you show a tool-tip on mouse-over to inform the user why the button is disabled?

推荐答案

Sam Mackrill,感谢您的回答,是使用Tag知道要离开的控件的好主意。但是,根据BobbyShaftoe的回答,您仍然需要IsShown标志。如果将鼠标放在错误的位置,如果ToolTip出现在其下面,则它可能引发另一个MouseMove事件(即使您没有实际移动鼠标)。这可能会导致一些不需要的动画,因为工具提示会不断消失并重新出现。

Sam Mackrill, thanks for your answer, great idea to use the Tag to know what Control you are leaving. However you still need the IsShown flag as per BobbyShaftoe's answer. If you have the mouse in the wrong spot, if the ToolTip comes up under it, it can fire another MouseMove event (even though you did not physically move the mouse). This can cause some unwanted animation, as the tooltip continually disappears and reappears.

这是我的代码:

    private bool toolTipShown = false;
    private void TimeWorks_MouseMove(object sender, MouseEventArgs e)
    {
        var parent = sender as Control;
        if (parent == null)
        {
            return;
        }
        var ctrl = parent.GetChildAtPoint(e.Location);
        if (ctrl != null)
        {
            if (ctrl.Visible && toolTip1.Tag == null)
            {
                if (!toolTipShown)
                {
                    var tipstring = toolTip1.GetToolTip(ctrl);
                    toolTip1.Show(tipstring.Trim(), ctrl, ctrl.Width / 2, ctrl.Height / 2);
                    toolTip1.Tag = ctrl;
                    toolTipShown = true;
                }
            }
        }
        else
        {
            ctrl = toolTip1.Tag as Control;
            if (ctrl != null)
            {
                toolTip1.Hide(ctrl);
                toolTip1.Tag = null;
                toolTipShown = false;
            }
        }
    }

这篇关于如何在禁用的按钮上显示工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆