首页 > 解决方案 > 如何在winform中的禁用按钮上显示工具提示?

问题描述

我有一个带有与之关联的工具提示的按钮,在启用按钮时可以正常工作。但我的要求是在禁用按钮时显示工具提示。我尝试了多种互联网上可用的解决方案,但没有解决方案有效。有任何想法吗?

我试过下面的代码:

public partial class Form1 : Form
    {
        private ToolTip _toolTip = new ToolTip();
        private Control _currentToolTipControl = null;

        public Form1()
        {
            InitializeComponent();

            _toolTip.SetToolTip(this.button1, "My button1");
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Control control = GetChildAtPoint(e.Location);
            if (control != null)
            {
                if (!control.Enabled && _currentToolTipControl == null)
                {
                    string toolTipString = _toolTip.GetToolTip(control);
                    // trigger the tooltip with no delay and some basic positioning just to give you an idea
                    _toolTip.Show(toolTipString, control, control.Width / 2, control.Height / 2);
                    _currentToolTipControl = control;
                }
            }
            else
            {
                if (_currentToolTipControl != null) _toolTip.Hide(_currentToolTipControl);
                _currentToolTipControl = null;
            }
        }
    }
}

标签: c#winformstooltip

解决方案


推荐阅读