I have some controls that I would like to display a ToolTip
for when the mouse is hovering over it. How can I do this? I would like to know how to do this properly in code, but also in the designer (There is a ToolTip
component in the toolbox, but I don't quite.. get it).
我有一些控件,我想在鼠标悬停的时候显示一个工具提示。我该怎么做呢?我想知道如何在代码中正确地做到这一点,但是在设计器中也是如此(工具箱中有一个工具提示组件,但我不太清楚)。得到它)。
I wouldn't be surprised if this is a duplicate, but I can only find questions that are on more advanced, specific scenarios. I would like to know the basics.
如果这是一份副本,我不会感到惊讶,但我只能找到那些更高级的、特定的场景的问题。我想知道基本情况。
5 个解决方案
#1
169
Here is your article for doing it with code
这是您的文章,用代码来做。
private void Form1_Load(object sender, System.EventArgs e)
{
// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();
// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;
// Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(this.button1, "My button1");
toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
}
#2
117
Drag a tooltip control from the toolbox onto your form. You don't really need to give it any properties other than a name. Then, in the properties of the control you wish to have a tooltip on, look for a new property with the name of the tooltip control you just added. It will by default give you a tooltip when the cursor hovers the control.
将工具提示控件从工具箱拖动到窗体上。除了名字之外,你不需要给它任何属性。然后,在控件的属性中,希望有一个工具提示,查找一个新属性,并添加刚才添加的工具提示控件的名称。当光标悬停控制时,默认情况下会给你一个工具提示。
#3
36
- Add a ToolTip component to your form
- 在表单中添加一个工具提示组件。
- Select one of the controls that you want a tool tip for
- 选择您想要工具提示的控件之一。
- Open the property grid (F4), in the list you will find a property called "ToolTip on toolTip1" (or something similar). Set the desired tooltip text on that property.
- 打开属性网格(F4),在列表中,您将找到一个名为“toolTip1”的属性(或类似的东西)。在该属性上设置所需的工具提示文本。
- Repeat 2-3 for the other controls
- 对其他控件重复2-3次。
- Done.
- 完成了。
The trick here is that the ToolTip control is an extender control, which means that it will extend the set of properties for other controls on the form. Behind the scenes this is achieved by generating code like in Svetlozar's answer. There are other controls working in the same manner (such as the HelpProvider
).
这里的诀窍是工具提示控件是一个扩展器控件,这意味着它将扩展窗体上其他控件的属性集。在幕后,这是通过生成像Svetlozar的答案中的代码来实现的。还有其他以相同方式工作的控件(例如HelpProvider)。
#4
5
ToolTip in C# is very easy to add to almost all UI controls. You don't need to add any MouseHover event for this.
c#中的工具提示非常容易添加到几乎所有的UI控件。您不需要为这个添加任何鼠标悬停事件。
This is how to do it-
这就是如何做到这一点。
-
Add a ToolTip object to your form. One object is enough for the entire form.
ToolTip toolTip = new ToolTip();
在窗体中添加一个工具提示对象。一个对象对于整个表单来说就足够了。工具提示=新的工具提示();
-
Add the control to the tooltip with the desired text.
使用所需的文本将控件添加到工具提示。
toolTip.SetToolTip(Button1,"Click here");
工具提示。SetToolTip(Button1,“点击这里”);
#5
-2
Just subscribe to the control's ToolTipTextNeeded event, and return e.TooltipText, much simpler.
只需订阅控件的tooltiptextrequired事件,并返回e。TooltipText,简单得多。
#1
169
Here is your article for doing it with code
这是您的文章,用代码来做。
private void Form1_Load(object sender, System.EventArgs e)
{
// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();
// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;
// Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(this.button1, "My button1");
toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
}
#2
117
Drag a tooltip control from the toolbox onto your form. You don't really need to give it any properties other than a name. Then, in the properties of the control you wish to have a tooltip on, look for a new property with the name of the tooltip control you just added. It will by default give you a tooltip when the cursor hovers the control.
将工具提示控件从工具箱拖动到窗体上。除了名字之外,你不需要给它任何属性。然后,在控件的属性中,希望有一个工具提示,查找一个新属性,并添加刚才添加的工具提示控件的名称。当光标悬停控制时,默认情况下会给你一个工具提示。
#3
36
- Add a ToolTip component to your form
- 在表单中添加一个工具提示组件。
- Select one of the controls that you want a tool tip for
- 选择您想要工具提示的控件之一。
- Open the property grid (F4), in the list you will find a property called "ToolTip on toolTip1" (or something similar). Set the desired tooltip text on that property.
- 打开属性网格(F4),在列表中,您将找到一个名为“toolTip1”的属性(或类似的东西)。在该属性上设置所需的工具提示文本。
- Repeat 2-3 for the other controls
- 对其他控件重复2-3次。
- Done.
- 完成了。
The trick here is that the ToolTip control is an extender control, which means that it will extend the set of properties for other controls on the form. Behind the scenes this is achieved by generating code like in Svetlozar's answer. There are other controls working in the same manner (such as the HelpProvider
).
这里的诀窍是工具提示控件是一个扩展器控件,这意味着它将扩展窗体上其他控件的属性集。在幕后,这是通过生成像Svetlozar的答案中的代码来实现的。还有其他以相同方式工作的控件(例如HelpProvider)。
#4
5
ToolTip in C# is very easy to add to almost all UI controls. You don't need to add any MouseHover event for this.
c#中的工具提示非常容易添加到几乎所有的UI控件。您不需要为这个添加任何鼠标悬停事件。
This is how to do it-
这就是如何做到这一点。
-
Add a ToolTip object to your form. One object is enough for the entire form.
ToolTip toolTip = new ToolTip();
在窗体中添加一个工具提示对象。一个对象对于整个表单来说就足够了。工具提示=新的工具提示();
-
Add the control to the tooltip with the desired text.
使用所需的文本将控件添加到工具提示。
toolTip.SetToolTip(Button1,"Click here");
工具提示。SetToolTip(Button1,“点击这里”);
#5
-2
Just subscribe to the control's ToolTipTextNeeded event, and return e.TooltipText, much simpler.
只需订阅控件的tooltiptextrequired事件,并返回e。TooltipText,简单得多。