tags:

views:

1264

answers:

3

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?

+4  A: 

So assuming your control is called "button1" you could do something like this. You have to do it by handling the MouseMove event of your form since the events won't be fired from your control.

bool IsShown = false;      

        void Form1_MouseMove(object sender, MouseEventArgs e)
        {
           Control ctrl = this.GetChildAtPoint(e.Location);

           if (ctrl != null)
           {
               if (ctrl == this.button1 && !IsShown)
               {
                   string tipstring =
this.toolTip1.GetToolTip(this.button1);                 
                   this.toolTip1.Show(tipstring, this.button1,
this.button1.Width /2, this.button1.Height / 2);
                   IsShown = true;
               }
           }
           else
           {
               this.toolTip1.Hide(this.button1);
               IsShown = false;
           }
        }
BobbyShaftoe
Spot on, thanks!
Sam Mackrill
+1  A: 

Here you can find two possible solutions for your problem

Stefano Driussi
That's an interesting method. It's a little more involved but it could definitely be preferable for some people.
BobbyShaftoe
+2  A: 

I have since adapted BobbyShaftoe's answer to be a bit more general

Notes:

  • The MouseMove event must be set on the parent control (a panel in my case)

    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)
            {
                var tipstring = toolTip1.GetToolTip(ctrl);
                toolTip1.Show(tipstring, ctrl, ctrl.Width / 2, ctrl.Height / 2);
                toolTip1.Tag = ctrl;
            }
        }
        else
        {
            ctrl = toolTip1.Tag as Control;
            if (ctrl != null)
            {
                toolTip1.Hide(ctrl);
                toolTip1.Tag = null;
            }
        }
    
    
    }
    
Sam Mackrill