微软官网就有示例代码: http://msdn.microsoft.com/zh-cn/library/8f3wy7cw(v=vs.80).aspx// This example assumes that the Form_Load event handling method // is connected to the Load event of the form. 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.AutoPopDelay = 5000; toolTip1.InitialDelay = 500; toolTip1.ReshowDelay = 500;
toolTip1.ShowAlways = true;
toolTip1.SetToolTip(this.button1, "yyyyyyy");
然后你的button按钮就有这个属性了
直接改要显示的提示内容就ok了
toolTip1.SetToolTip(要绑定控件的对象,需提示的内容);
C#中提供了信息提示框,这有很多用处,可以提示控件或者用户自定义的属性信息,而且可以自动弹出或者用户指定弹出,也可以动画效果弹出。
但是这个控件设计和实现有很多不好的地方,使用的时候需要一些技巧。
二、使用方法
(1)鼠标移动到控件或指定的位置自动显示
ToolTip.SetToolTip 方法 ,注意相关参数的设置。
(2)动画效果
参数:AutoPopDelay InitialDelay ReshowDelay
例如:
ToolTip toolTip1 = new ToolTip();
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
toolTip1.ShowAlways = true;
toolTip1.SetToolTip(this.button1, "My button1");
toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
(3)提示图片设置
ToolTipIcon属性
Error 错误图标
Info 信息图标。
None 不是标准图标。
Warning 警告图标。
(4)气泡样式
ToolTip.IsBalloon 属性 设置为True
(5)自定义位置和触发事件显示
ToolTip.Show 方法,注意相关参数的设置。
例如:m_ToolTip.Show(pStringBuilder.ToString(),
m_HookHelperJP.FormObjects.Win32Window,e.x + r.Left, e.y + r.Top);
三、使用技巧
(1)ToolTip.Show时显示不能隐藏问题
可做以下处理:在再次触发Show前进行Hide操作
(2)m_ToolTip信息不能及时更新问题
这是因为其内存清理存在问题,处理方法:在显示前清楚内存,重新实例化。
例如:
m_ToolTip.Dispose();
m_ToolTip = new ToolTip();
m_ToolTip.ToolTipIcon = ToolTipIcon.Info;
//m_ToolTip.IsBalloon = true;
m_ToolTip.ShowAlways = true;
m_ToolTip.ToolTipTitle = sName;
m_ToolTip.Show(pStringBuilder.ToString(),
m_HookHelperJP.FormObjects.Win32Window, e.x + r.Left, e.y + r.Top);
(3)固定宽度设置
ToolTip没有直接提供Width属性,找了很久可用以下方法设置:
在显示前ToolTip.Popup 事件
中通过参数PopupEventArgs.Size进行设置。但是这只能覆盖多出的信息。这种处理不合理。
建议另外一种方法:设置每一行固定字符,多出的则换行显示
{
InitTooltip(this.toolTip1);
} private void InitTooltip(ToolTip tooltip)
{
tooltip.AutomaticDelay = 300; //延迟300毫秒提示
tooltip.AutoPopDelay = 1000; //提示信息在控件上的显示时间为1秒
tooltip.InitialDelay = 100; //显示前经过100毫秒
tooltip.IsBalloon = true;
tooltip.OwnerDraw = true;
tooltip.ReshowDelay = 100;
tooltip.ShowAlways = false;
tooltip.ToolTipIcon = ToolTipIcon.Info; //显示信息图标
tooltip.ToolTipTitle = "提示:";
tooltip.UseAnimation = true;
tooltip.UseFading = true;
tooltip.RemoveAll(); foreach (Control c in this.Controls)
{
tooltip.SetToolTip(c, "控件的类型是:" + c.GetType().Name);
} }
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
toolTip1.ShowAlways = true;
toolTip1.SetToolTip(this.button1, "My button1");
//tooltip.show("提示信息",控件名,持续时间);
tooltip.Show(" 0-240.0 ", textBox22, 1800);
http://msdn.microsoft.com/zh-cn/library/8f3wy7cw(v=vs.80).aspx// This example assumes that the Form_Load event handling method
// is connected to the Load event of the form.
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");
}
ToolTip
MSDN才是王道
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.tooltip(v=VS.80).aspx