如何在C#Winforms中为标签添加提示或工具提示?

时间:2012-03-19 18:52:07

标签: c# .net winforms label tooltip

似乎Label没有HintToolTipHovertext属性。那么当鼠标逼近Label时,显示提示,工具提示或悬停文本的首选方法是什么?

5 个答案:

答案 0 :(得分:102)

您必须先向表单添加ToolTip控件。然后你可以设置它应该为其他控件显示的文本。

以下是在添加名为ToolTip的{​​{1}}控件后显示设计器的屏幕截图:

enter image description here

答案 1 :(得分:75)

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.");

答案 2 :(得分:20)

System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip( Label1, "Label for Label1");

答案 3 :(得分:13)

只是另一种方式。

Label lbl = new Label();
new ToolTip().SetToolTip(lbl, "tooltip text here");

答案 4 :(得分:4)

只是为了分享我的想法......

我创建了一个自定义类来继承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);
            }
        }
    }

在表单或用户控件的InitializeComponent方法(Designer代码)中,将Label控件重新分配给自定义类:

this.lblMyLabel = new ucLabel();

此外,更改Designer代码中的私有变量引用:

private ucLabel lblMyLabel;