Skip to content
Related Articles

Related Articles

C# | ToolTip Class
  • Last Updated : 05 Sep, 2019
GeeksforGeeks - Summer Carnival Banner

In Windows Forms, the ToolTip represents a tiny pop-up box which appears when you place your pointer or cursor on the control and the purpose of this control is it provides a brief description about the control present in the windows form. The ToolTip class is used to create ToolTip control and also provide different types of properties, methods, events and also provides run time status of the controls.
You are allowed to use a ToolTip class in any container or control. With the help of a single ToolTip component, you are allowed to create multiple tooltips for multiple controls. the ToolTip class defined under System.Windows.Forms namespace. In C# you can create a ToolTip in the windows form by using two different ways:

1. Design-Time: It is the easiest way to create a ToolTip as shown in the following steps:

  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Drag the ToolTip from the ToolBox and drop it on the form. When you drag and drop this ToolTip on the form it will automatically add to the properties(named as ToolTip on ToolTip1) of every controls present in the current windows from.
  • Step 3: After drag and drop you will go to the properties of the ToolTip control to modify ToolTip according to your requirement.

    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a ToolTip control programmatically with the help of syntax provided by the ToolTip class. The following steps show how to set the create ToolTip dynamically:

  • Step 1: Create a ToolTip control using the ToolTip() constructor is provided by the ToolTip class.
    // Creating a ToolTip control
    ToolTip t_Tip = new ToolTip(); 
    
  • Step 2: After creating ToolTip control, set the property of the ToolTip control provided by the ToolTip class.
    // Seting the properties of ToolTip
    t_Tip.Active = true; 
    t_Tip.AutoPopDelay = 4000; 
    t_Tip.InitialDelay = 600; 
    t_Tip.IsBalloon = true; 
    t_Tip.ToolTipIcon = ToolTipIcon.Info; 
    t_Tip.SetToolTip(box1, "Name should start with Capital letter"); 
    t_Tip.SetToolTip(box2, "Password should be greater than 8 words"); 
    

    Example:




    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp34 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the
            // properties of the Label
            Label l1 = new Label();
            l1.Location = new Point(140, 122);
            l1.Text = "Name";
      
            // Adding this Label
            // control to the form
            this.Controls.Add(l1);
      
            // Creating and setting the
            // properties of the TextBox
            TextBox box1 = new TextBox();
            box1.Location = new Point(248, 119);
            box1.BorderStyle = BorderStyle.FixedSingle;
      
            // Adding this TextBox
            // control to the form
            this.Controls.Add(box1);
      
            // Creating and setting the
            // properties of Label
            Label l2 = new Label();
            l2.Location = new Point(140, 152);
            l2.Text = "Password";
      
            // Adding this Label
            // control to the form
            this.Controls.Add(l2);
      
            // Creating and setting the
            // properties of the TextBox
            TextBox box2 = new TextBox();
            box2.Location = new Point(248, 145);
            box2.BorderStyle = BorderStyle.FixedSingle;
      
            // Adding this TextBox
            // control to the form
            this.Controls.Add(box2);
      
            // Creating and setting the
            // properties of the ToolTip
            ToolTip t_Tip = new ToolTip();
            t_Tip.Active = true;
            t_Tip.AutoPopDelay = 4000;
            t_Tip.InitialDelay = 600;
            t_Tip.IsBalloon = true;
            t_Tip.ToolTipIcon = ToolTipIcon.Info;
            t_Tip.SetToolTip(box1, "Name should start with Capital letter");
            t_Tip.SetToolTip(box2, "Password should be greater than 8 words");
        }
    }
    }

    Output:

Constructor

ConstructorDescription
ToolTip()This Constructors is used to initialize a new instance of the ToolTip without a specified container.
ToolTip(IContainer)This Constructors is used to initialize a new instance of the ToolTip class with a specified container.

Properties

PropertyDescription
ActiveThis property is used to get or set a value indicating whether the ToolTip is currently active.
AutomaticDelayThis property is used to get or set the automatic delay for the ToolTip.
AutoPopDelayThis property is used to get or set the period of time the ToolTip remains visible if the pointer is stationary on a control with specified ToolTip text.
BackColorThis property is used to get or set the background color for the control.
ForeColorThis property is used to get or set the foreground color of the control.
InitialDelayThis property is used to get or set the time that passes before the ToolTip appears.
IsBalloonThis property is used to get or set a value indicating whether the ToolTip should use a balloon window.
ReshowDelayThis property is used to get or set the length of time that must transpire before subsequent ToolTip windows appear as the pointer moves from one control to another.
ToolTipIconThis property is used to get or set a value that defines the type of icon to be displayed alongside the ToolTip text.
ToolTipTitleThis property is used to get or set a title for the ToolTip window.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :