C# 如何在禁用的按钮上显示工具提示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/491267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I show a tooltip on a disabled button?
提问by Sam Mackrill
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?
如果您在 winform 上有一个禁用的按钮,您如何在鼠标悬停时显示工具提示以通知用户为什么该按钮被禁用?
采纳答案by Peter Smartt
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.
Sam Mackrill,感谢您的回答,使用标签来了解您要离开的控件的好主意。但是,根据 BobbyShaftoe 的回答,您仍然需要 IsShown 标志。如果您将鼠标放在错误的位置,如果工具提示出现在它的下方,它会触发另一个 MouseMove 事件(即使您没有实际移动鼠标)。这可能会导致一些不需要的动画,因为工具提示会不断消失并重新出现。
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;
}
}
}
回答by BobbyShaftoe
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.
所以假设你的控件被调用,button1
你可以做这样的事情。
您必须通过处理MouseMove
表单的事件来完成,因为这些事件不会从您的控件中触发。
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;
}
}
}
回答by Sam Mackrill
I have since adapted BobbyShaftoe's answer to be a bit more general
从那以后,我将 BobbyShaftoe 的答案改编得更笼统
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; } } }
MouseMove 事件必须在父控件上设置(在我的例子中是一个面板)
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; } } }
回答by user1172173
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.
将按钮(或适合此方案的任何控件)放在容器(面板、tableLayoutPanel)中,并将工具提示关联到相应的底层面板单元格。在许多场景中都能很好地工作,灵活。提示:使单元格足够大以容纳 bttn,因此鼠标悬停响应(工具提示显示)不会出现在 bttn 边界外“流血”。