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

时间:2021-06-14 20:55:14

It seems that the Label has no Hint or ToolTip or Hovertext property. So what is the preferred method to show a hint, tooltip, or hover text when the Label is approached by the mouse?

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

5 个解决方案

#1


91  

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

您必须首先向窗体添加工具提示控件。然后您可以设置它应该为其他控件显示的文本。

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

下面是添加了一个名为toolTip1的工具提示控件的设计器的屏幕截图:

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

#2


69  

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

#3


19  

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

#4


12  

just another way to do it.

这是另一种方法。

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

#5


4  

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类。我添加了一个作为工具提示类和公共属性TooltipText分配的私有变量。然后,给它一个MouseEnter委托方法。这是一种使用多个标签控件的简单方法,无需为每个标签控件分配工具提示控件。

    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方法(设计器代码)中,将标签控件重新分配给自定义类:

this.lblMyLabel = new ucLabel();

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

同时,改变设计器代码中的私有变量引用:

private ucLabel lblMyLabel;

#1


91  

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

您必须首先向窗体添加工具提示控件。然后您可以设置它应该为其他控件显示的文本。

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

下面是添加了一个名为toolTip1的工具提示控件的设计器的屏幕截图:

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

#2


69  

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

#3


19  

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

#4


12  

just another way to do it.

这是另一种方法。

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

#5


4  

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类。我添加了一个作为工具提示类和公共属性TooltipText分配的私有变量。然后,给它一个MouseEnter委托方法。这是一种使用多个标签控件的简单方法,无需为每个标签控件分配工具提示控件。

    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方法(设计器代码)中,将标签控件重新分配给自定义类:

this.lblMyLabel = new ucLabel();

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

同时,改变设计器代码中的私有变量引用:

private ucLabel lblMyLabel;