【发布时间】:2010-11-23 07:06:20
【问题描述】:
我想在鼠标悬停在控件上时显示ToolTip。
如何在代码中以及在设计器中创建工具提示?
【问题讨论】:
-
相关的,更旧的(一般适用于 .NET):stackoverflow.com/questions/168550/…
我想在鼠标悬停在控件上时显示ToolTip。
如何在代码中以及在设计器中创建工具提示?
【问题讨论】:
Here 是你写代码的文章
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");
}
【讨论】:
toolTip1 变量是否超出范围并不重要?
ToolTip 怎么样?
将工具提示控件从工具箱拖到表单上。除了名称之外,您实际上不需要给它任何属性。然后,在您希望在其上显示工具提示的控件的属性中,查找具有您刚刚添加的工具提示控件名称的新属性。默认情况下,当光标悬停在控件上时,它会为您提供工具提示。
【讨论】:
这里的诀窍在于,ToolTip 控件是一个扩展器控件,这意味着它将扩展表单上其他控件 的属性集。在幕后,这是通过生成像 Svetlozar's answer 中的代码来实现的。还有其他控件以相同的方式工作(例如HelpProvider)。
【讨论】:
C# 中的 ToolTip 非常容易添加到几乎所有 UI 控件中。您不需要为此添加任何 MouseHover 事件。
这是怎么做的-
将 ToolTip 对象添加到表单中。一个对象就足够了整个表单。
ToolTip toolTip = new ToolTip();
将控件添加到带有所需文本的工具提示中。
toolTip.SetToolTip(Button1,"Click here");
【讨论】:
我是这样做的:只需将事件添加到任何控件,设置控件的标签,然后添加一个条件来处理相应控件/标签的工具提示。
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);
}
}
【讨论】:
只需订阅控件的ToolTipTextNeeded事件,并返回e.TooltipText,就简单多了。
【讨论】: