C# 如何向控件添加工具提示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1339524/
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 do I add a ToolTip to a control?
提问by Svish
I have some controls that I would like to display a ToolTip
for when the mouse is hovering over it. How can I do this? I would like to know how to do this properly in code, but also in the designer (There is a ToolTip
component in the toolbox, but I don't quite.. get it).
我有一些控件,ToolTip
当鼠标悬停在它上面时,我想显示它们。我怎样才能做到这一点?我想知道如何在代码中正确执行此操作,但也想知道如何在设计器中正确执行此操作(ToolTip
工具箱中有一个组件,但我不太明白……明白了)。
I wouldn't be surprised if this is a duplicate, but I can only find questions that are on more advanced, specific scenarios. I would like to know the basics.
如果这是重复的,我不会感到惊讶,但我只能找到关于更高级、特定场景的问题。我想知道基本情况。
采纳答案by Svetlozar Angelov
Hereis your article for doing it with code
这是你用代码做的文章
private void Form1_Load(object sender, System.EventArgs e)
{
// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();
// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;
// Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(this.button1, "My button1");
toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
}
回答by JYelton
Drag a tooltip control from the toolbox onto your form. You don't really need to give it any properties other than a name. Then, in the properties of the control you wish to have a tooltip on, look for a new property with the name of the tooltip control you just added. It will by default give you a tooltip when the cursor hovers the control.
将工具提示控件从工具箱拖到窗体上。除了名称之外,您实际上不需要为其提供任何属性。然后,在您希望显示工具提示的控件的属性中,使用您刚刚添加的工具提示控件的名称查找新属性。默认情况下,当光标悬停在控件上时,它会为您提供工具提示。
回答by Fredrik M?rk
- Add a ToolTip component to your form
- Select one of the controls that you want a tool tip for
- Open the property grid (F4), in the list you will find a property called "ToolTip on toolTip1" (or something similar). Set the desired tooltip text on that property.
- Repeat 2-3 for the other controls
- Done.
- 将 ToolTip 组件添加到您的表单
- 选择您想要工具提示的控件之一
- 打开属性网格 ( F4),在列表中您会找到一个名为“ToolTip on toolTip1”(或类似内容)的属性。在该属性上设置所需的工具提示文本。
- 对其他控件重复 2-3
- 完毕。
The trick here is that the ToolTip control is an extender control, which means that it will extend the set of properties for other controlson the form. Behind the scenes this is achieved by generating code like in Svetlozar's answer. There are other controls working in the same manner (such as the HelpProvider
).
这里的技巧是 ToolTip 控件是一个扩展控件,这意味着它将扩展表单上其他控件的属性集。在幕后,这是通过像 Svetlozar 的回答那样生成代码来实现的。还有其他控件以相同的方式工作(例如HelpProvider
)。
回答by fredv
Just subscribe to the control's ToolTipTextNeeded event, and return e.TooltipText, much simpler.
只需订阅控件的ToolTipTextNeeded 事件,并返回e.TooltipText,就简单多了。
回答by OopsDev
ToolTip in C# is very easy to add to almost all UI controls. You don't need to add any MouseHover event for this.
C# 中的 ToolTip 很容易添加到几乎所有 UI 控件中。您不需要为此添加任何 MouseHover 事件。
This is how to do it-
这是怎么做的-
Add a ToolTip object to your form. One object is enough for the entire form.
ToolTip toolTip = new ToolTip();
Add the control to the tooltip with the desired text.
toolTip.SetToolTip(Button1,"Click here");
将 ToolTip 对象添加到您的表单。一个对象足以容纳整个表单。
ToolTip toolTip = new ToolTip();
将控件添加到带有所需文本的工具提示。
toolTip.SetToolTip(Button1,"Click here");
回答by log-cab.in
I did it this way: Just add the event to any control, set the control's tag, and add a conditional to handle the tooltip for the appropriate control/tag.
我是这样做的:只需将事件添加到任何控件,设置控件的标签,然后添加一个条件来处理相应控件/标签的工具提示。
private void Info_MouseHover(object sender, EventArgs e)
{
Control senderObject = sender as Control;
string hoveredControl = senderObject.Tag.ToString();
// only instantiate a tooltip if the control's tag contains data
if (hoveredControl != "")
{
ToolTip info = new ToolTip
{
AutomaticDelay = 500
};
string tooltipMessage = string.Empty;
// add all conditionals here to modify message based on the tag
// of the hovered control
if (hoveredControl == "save button")
{
tooltipMessage = "This button will save stuff.";
}
info.SetToolTip(senderObject, tooltipMessage);
}
}