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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9776077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:49:00  来源:igfitidea点击:

How can I add a hint or tooltip to a label in C# Winforms?

c#.netwinformslabeltooltip

提问by B. Clay Shannon

It seems that the Labelhas no Hintor ToolTipor Hovertextproperty. So what is the preferred method to show a hint, tooltip, or hover text when the Labelis approached by the mouse?

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

采纳答案by Yuck

You have to add a ToolTipcontrol to your form first. Then you can set the text it should display for other controls.

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

Here's a screenshot showing the designer after adding a ToolTipcontrol which is named toolTip1:

这是添加ToolTip名为的控件后显示设计器的屏幕截图toolTip1

enter image description here

在此处输入图片说明

回答by scibuff

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

回答by SeeSharp

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

回答by ac0de

just another way to do it.

只是另一种方式来做到这一点。

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

回答by cChacon

Just to share my idea...

只是分享我的想法...

I created a custom class to inherit the Label class. I added a private variable assigned as a Tooltip class and a public property, TooltipText. Then, gave it a MouseEnter delegate method. This is an easy way to work with multiple Label controls and not have to worry about assigning your Tooltip control for each Label control.

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

In the form or user control's InitializeComponent method (the Designer code), reassign your Label control to the custom class:

在窗体或用户控件的 InitializeComponent 方法(设计器代码)中,将您的 Label 控件重新分配给自定义类:

this.lblMyLabel = new ucLabel();

Also, change the private variable reference in the Designer code:

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

private ucLabel lblMyLabel;