See*_*arp 81
yourToolTip = new ToolTip();
//The below are optional, of course,
yourToolTip.ToolTipIcon = ToolTipIcon.Info;
yourToolTip.IsBalloon = true;
yourToolTip.ShowAlways = true;
yourToolTip.SetToolTip(lblYourLabel,"Oooh, you put your mouse over me.");
Run Code Online (Sandbox Code Playgroud)
sci*_*uff 20
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip( Label1, "Label for Label1");
Run Code Online (Sandbox Code Playgroud)
ac0*_*0de 13
只是另一种方式来做到这一点.
Label lbl = new Label();
new ToolTip().SetToolTip(lbl, "tooltip text here");
Run Code Online (Sandbox Code Playgroud)
小智 5
只是分享我的想法...
我创建了一个自定义类来继承Label类。我添加了一个分配为Tooltip类的私有变量和一个公共属性TooltipText。然后,给它一个MouseEnter委托方法。这是使用多个Label控件的简便方法,而不必担心为每个Label控件分配Tooltip控件。
public partial class ucLabel : Label
{
private ToolTip _tt = new ToolTip();
public string TooltipText { get; set; }
public ucLabel() : base() {
_tt.AutoPopDelay = 1500;
_tt.InitialDelay = 400;
// _tt.IsBalloon = true;
_tt.UseAnimation = true;
_tt.UseFading = true;
_tt.Active = true;
this.MouseEnter += new EventHandler(this.ucLabel_MouseEnter);
}
private void ucLabel_MouseEnter(object sender, EventArgs ea)
{
if (!string.IsNullOrEmpty(this.TooltipText))
{
_tt.SetToolTip(this, this.TooltipText);
_tt.Show(this.TooltipText, this.Parent);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在窗体或用户控件的InitializeComponent方法(设计器代码)中,将Label控件重新分配给自定义类:
this.lblMyLabel = new ucLabel();
Run Code Online (Sandbox Code Playgroud)
另外,在设计器代码中更改私有变量引用:
private ucLabel lblMyLabel;
Run Code Online (Sandbox Code Playgroud)