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

B. *_*non 76 .net c# label tooltip winforms

看来,Label没有HintToolTipHovertext财产.那么当Label鼠标逼近时,显示提示,工具提示或悬停文本的首选方法是什么?

Yuc*_*uck 105

您必须ToolTip首先向表单添加控件.然后,您可以设置应显示其他控件的文本.

这是一个屏幕截图,显示设计师在添加一个ToolTip名为的控件后toolTip1:

在此输入图像描述

  • 哇,这看起来很复杂/违反直觉,哎呀. (18认同)
  • 我同意。它还允许您对多个控件使用相同的工具提示控件。 (2认同)

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)