0

I have a button with a tooltip associated to it which works fine when button is enabled. But my requirement is to show the tooltip when button is disabled. I have tried multiple solutions available over internet but no solution is working. Any ideas?

I have tried below code:

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;
            }
        }
    }
}
Raj
  • 9
  • 6
  • Test your code, but tooltip shown normally when button disabled. Did you subscribe to `MouseMove` correctly? – 大陸北方網友 Sep 28 '20 at 06:31
  • "subscribe to MouseMove" means ? Please tell me what I need to do – Raj Sep 28 '20 at 06:34
  • 1
    Refer to this document [How to subscribe to and unsubscribe from events](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events). Try to add `this.MouseMove += Form1_MouseMove;` in `Form1()`. – 大陸北方網友 Sep 28 '20 at 06:37
  • Glad to hear that. – 大陸北方網友 Sep 28 '20 at 06:58
  • The code seems to be taken from [this answer](https://stackoverflow.com/a/1732845/1997232), referring to source would help the answerer, consider always add it. – Sinatr Sep 28 '20 at 07:41

0 Answers0