How to create ToolTip for a Windows Form Control using Visual C# (in .NET Framework)

How to create ToolTip for a Windows Form Control using Visual C# (in .NET Framework)

Sometimes, we may want to display important help text to the user when he or she hovers over a control. For example, you may have a label in your application and you want that the user should know upon hovering what the label text signifies. This is when ToolTip comes into picture. Tooltip is basically a small “hover box” in which the information about the item being hovered upon is displayed. Here, we will learn how to add ToolTip to a windows form control (consisting of labels and textboxes) using C# (in .NET framework). Suppose we have a label as Salary and corresponding to it two textboxes in which some random figures are getting displayed, as shown below:

Windows form control

Now, we want that whenever a user hovers on the label and textboxes then some help text should get displayed. For example, here we are displaying salary figure of an employee. The first textbox displays salary in $ and second textbox in Rs. To create tooltip for these three controls, open the code of the form and paste the following code in it:

private void myToolTip()
        {
            ToolTip myToolTip = new ToolTip();
            myToolTip.SetToolTip(textBox1, "in $");
            myToolTip.SetToolTip(textBox2, " in Rs");
            myToolTip.SetToolTip(label1, "Per Month");
        }

Note: Make sure you have the System.Windows.Forms namespace added at the beginning of the code.

Now, in order to setup the tooltip for all these three controls we just need to call this function when this form loads. Lets add this immediately after initialization of the form components. See the complete code below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyToolTip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "50";
            textBox2.Text = "250";
            myToolTip();

        }
        private void myToolTip()
        {
            ToolTip myToolTip = new ToolTip();
            myToolTip.SetToolTip(textBox1, "in $");
            myToolTip.SetToolTip(textBox2, " in Rs");
            myToolTip.SetToolTip(label1, "Per Month");
        }
    }
}

Save the code and run it. Now, move your cursor over the label and textboxes. You will see the tooltip is displayed, as shown below:

Window form tooltip for label
Visula Studion C# .NET Windows Form displaying tool tip
Visual Studio C# .NET displaying textbox tooltip in windows form

Explanation of the code:

Here, we have used the ToolTip class of the  System.Windows.Forms namespace. This class represents a small rectangular pop-up window which is used to specifically display a control’s purpose when user hovers over it. Here, first we make an object of the ToolTip class and then using the SetToolTip function we set the information to be displayed over the control. The SetToolTip function takes in two arguments, the first is the control name and the second is the caption to be displayed.

ToolTip myToolTip = new ToolTip(); 
myToolTip.SetToolTip(textBox1, "in $");

Using the above code, we have passed the control name (for example textBox1), and the text to be displayed upon hovering (for example “in $”) as arguments in the SetToolTip function. Upon running the code it renders the tooltip text upon hovering.

In a similar fashion, you may add ToolTips to other windows form controls of your choice.