禁用按钮上的工具提示 [英] Tooltip on disabled button

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

问题描述

我在某个时候发布了这个问题,有人将其标记为重复.但是当我问他关于据称有答案的帖子的问题时,他没有任何适当的解释.因此,请在标记或否决投票之前仔细阅读此问题.

I posted this question sometime back and someone marked it as DUPLICATE. But when i asked him questions about the post that supposedly has the answer, he didn't had any proper explanations. So please read this question carefully before marking or down voting.

我的问题是关于在禁用的按钮上显示工具提示.

My question is regarding displaying tooltip on a Button that's DISABLED.

this.btnMy.Enabled = false;

我有一个按钮,它被放置在一个面板内,并带有与之关联的工具提示.现在启用按钮后一切正常.

I have a button which is placed inside a panel with a tooltip associated to it. Now it all works fine when button is enabled.

但如果按钮禁用,则工具提示不起作用.这似乎是标准行为.

But if button is disabled, then tooltip doesn't work. This seems to be the standard behavior.

现在我也想在按钮禁用时显示工具提示.所以我做了以下事情.

Now i want to show the tooltip when button is disabled too. So i did the following thing.

private ToolTip m_tooltip = new ToolTip();
private bool toolTipShown = false;
private button btnMy;

m_tooltip.InitialDelay = 0;
m_tooltip.ShowAlways = true;

 private void myForm_MouseMove(object sender, MouseEventArgs e)
        {

            if (this.btnMy == FindControlAtCursor(this))
            {
                if (!toolTipShown)
                {
                    m_tooltip.Show("MyToolTip", this.btnMy, e.Location);
                    toolTipShown = true;
                }
            }
            else
            {
                m_tooltip.Hide(this.btnGiven);
                toolTipShown = false;
            }
        }

由于按钮在面板内,当鼠标悬停在它上面时,我不得不使用其他几个功能来找到确切的按钮控件.

As the button is inside a panel, i had to use couple other functions to find the exact button control when mouse is over it.

public static Control FindControlAtPoint(Control container, Point pos)
        {
            Control child;
            foreach (Control c in container.Controls)
            {
                if (c.Visible && c.Bounds.Contains(pos))
                {
                    child = FindControlAtPoint(c, new Point(pos.X - c.Left, pos.Y - c.Top));
                    if (child == null) return c;
                    else return child;
                }
            }
            return null;
        }

        public static Control FindControlAtCursor(Form form)
        {
            Point pos = Cursor.Position;
            if (form.Bounds.Contains(pos))
                return FindControlAtPoint(form, form.PointToClient(Cursor.Position));
            return null;
        }

现在,当我调试时,我可以看到代码找到了正确的按钮并尝试调用 ToolTip.Show,但由于某种原因它没有显示出来.

Now when i debug, i can see the code finding the right button and trying to call ToolTip.Show, but for some reason it's not getting displayed.

在调试时,我看到一个小工具提示弹出.否则在发布模式下,什么都不会显示.

While debugging, i see a small tooltip pop up. Other wise in release mode, nothing gets shown at all.

有什么想法吗?

推荐答案

我不打算争论它是否重复(尽管作为 HABO 在评论中指出它看起来与 如何在禁用的按钮上显示工具提示?).另外我不打算讨论整个代码.

I'm not going to argue whether it's a duplicate or not (although as HABO pointed out in the comments it looks pretty similar to How can I show a tooltip on a disabled button?). Also I'm not going to discuss the code in whole.

主要问题在这一行

m_tooltip.Show("MyToolTip", this.btnMy, e.Location);

文档ToolTip.Show 重载您正在使用 point 参数的状态

The documentation for the ToolTip.Show overload that you are using states for the point argument

包含偏移量的点,以像素为单位,相对于关联控件窗口的左上角,用于显示工具提示.

A Point containing the offset, in pixels, relative to the upper-left corner of the associated control window, to display the ToolTip.

当你传递一个相对于表单的点时.

while you are passing a point relative to the form.

为了修复它,你应该使用这样的东西

In order to fix it, you should use something like this

var pos = this.btnMy.PointToClient(this.PointToScreen(e.Location));
m_tooltip.Show("MyToolTip", this.btnMy, pos);

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

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