How to use the ToolTip control in WinForm?

The ToolTip control is used to display information when the mouse hovers over its associated control, showing a tooltip box.

To use the ToolTip control in WinForm, first drag the ToolTip control onto the form in the designer. Then, set the properties and associate controls with the ToolTip control through code.

Here is an example code demonstrating how to use the ToolTip control in WinForms.

using System;
using System.Windows.Forms;

namespace ToolTipExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            // 创建一个ToolTip控件对象
            ToolTip toolTip1 = new ToolTip();
            
            // 设置ToolTip控件的属性
            toolTip1.AutoPopDelay = 5000; // 提示框显示的时间
            toolTip1.InitialDelay = 1000; // 鼠标悬停后多久显示提示框
            toolTip1.ReshowDelay = 500; // 提示框重新显示的时间
            toolTip1.ShowAlways = true; // 是否总是显示提示框
            
            // 给一个控件关联ToolTip控件
            toolTip1.SetToolTip(button1, "这是一个按钮");
        }
    }
}

In the example above, we first create a ToolTip control object, then set some properties, and finally associate the ToolTip control with a button control using the SetToolTip method. When the mouse hovers over the button, a tooltip displaying “This is a button” will appear.

By using this method, we can easily display tooltip messages in WinForm using the ToolTip control.

Leave a Reply 0

Your email address will not be published. Required fields are marked *