How can I show a tooltip on a disabled button?

后端 未结 4 724
臣服心动
臣服心动 2020-12-03 00:40

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条回答
  • 2020-12-03 01:21

    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;
       }
    

    }

    0 讨论(0)
  • 2020-12-03 01:26

    Place the button (or any control that fits this scenario) in a container (panel, tableLayoutPanel), and associate the tooltip to the appropriate underlying panel cell. Works great in a number of scenarios, flexible. Tip: make the cell just large enough to hold the bttn, so mouseover response (tooltip display) doesn't appear to "bleed" outside the bttn borders.

    0 讨论(0)
  • 2020-12-03 01:27

    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 && !ctrl.Enabled)
          {
              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;
              }
          }
      
      }
      
    0 讨论(0)
  • 2020-12-03 01:38

    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.

    Here is my code:

        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;
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题