How can I show a tooltip on a disabled button?

后端 未结 4 740
臣服心动
臣服心动 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: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;
              }
          }
      
      }
      

提交回复
热议问题