Using ToolTip control in Windows Form


In this article I will explain use of tooltips on Mouse Hover, and some other events like Textbox enter and others.

Generally we can see tooltip in this fashion

1.gif 

I will show this in a balloon fashioned tooltip so that the look and feel of this will be good as follows

2.gif 

Example:

Design a form in such a way that you should have some labels, textboxes, and combo box as per your requirement.

In my example I will take 3 textboxes, 3 labels 1 combo box a button and a Tooltip.

Sample Screen:

3.gif
 
Output

4.gif
 
Since I have written my code in the mouse hover event the tool tips will appear if the mouse is over a particular control

Code 

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

namespace Tooltip
{
    public partial class Form1 : Form
    {
        Required requiredField = new Required();
        RequiredLength reqLenth = new RequiredLength();
        MouseOver objMouseover = new MouseOver();
        MouseLeave objMouseleave = new MouseLeave();
        ToolTipRemove ttpRemove = new ToolTipRemove();
        public Form1()
        {
            InitializeComponent();
        }
        public class Required
        {
            public void Requiredfield(TextBox txtRequired, ToolTip ttpRequired)
            {
                if (txtRequired.Text != string.Empty)
                {
                    ttpRequired.ShowAlways = false;
                    ttpRequired.Hide(txtRequired);
                }
                else
                {
                    ttpRequired.AutoPopDelay = 2000;
                    ttpRequired.InitialDelay = 1000;
                    ttpRequired.ReshowDelay = 500;
                    ttpRequired.IsBalloon = true;
                    ttpRequired.ShowAlways = true;
                    ttpRequired.Show(string.Empty, txtRequired, 0);
                    ttpRequired.Show("Required", txtRequired, txtRequired.Width, txtRequired.Height / 10);
                }
            }
        }
        public class RequiredLength
        {
            public void reqLength(TextBox txtLength, ToolTip ttpLength)
            {
                if (txtLength.Text.Length < 9)
                {
                    ttpLength.AutoPopDelay = 2000;
                    ttpLength.InitialDelay = 1000;
                    ttpLength.ReshowDelay = 500;
                    ttpLength.IsBalloon = true;
                    ttpLength.ShowAlways = true;
                    ttpLength.SetToolTip(txtLength, "Must be 10 Digits");
                    ttpLength.Show("Must be 10 Digits", txtLength, txtLength.Width, txtLength.Height / 10);
                }
                else
                {
                    ttpLength.ShowAlways = false;
                    ttpLength.Hide(txtLength);
                }
            }
        }
        public class MouseOver
        {
            public void mouseOver(TextBox txtShow, ToolTip ttpShow)
            {
                switch (txtShow.Name)
                {
                    case "textBox1":
                        {
                            ttpShow.AutoPopDelay = 2000;
                            ttpShow.InitialDelay = 1000;
                            ttpShow.ReshowDelay = 500;
                            ttpShow.IsBalloon = true;
                            ttpShow.SetToolTip(txtShow, "Ex:Dorababu");
                            ttpShow.Show("Ex : Dorabbau", txtShow, txtShow.Width, txtShow.Height / 10, 5000);
                        }
                        break;
                    case "textBox2":
                        {
                            ttpShow.AutoPopDelay = 2000;
                            ttpShow.InitialDelay = 1000;
                            ttpShow.ReshowDelay = 500;
                            ttpShow.IsBalloon = true;
                            ttpShow.SetToolTip(txtShow, "Ex:9000000000(Must be 10 Digits");
                            ttpShow.Show("Ex:9000000000(Must be 10 Digits", txtShow, txtShow.Width, txtShow.Height / 10, 5000);
                        }
                        break;
                    case "textBox3":
                        {
                            ttpShow.AutoPopDelay = 2000;
                            ttpShow.InitialDelay = 1000;
                            ttpShow.ReshowDelay = 500;
                            ttpShow.IsBalloon = true;
                            ttpShow.SetToolTip(txtShow, "Ex:XYZ,India");
                            ttpShow.Show("Ex:XYZ,India", txtShow, txtShow.Width, txtShow.Height / 10, 5000);
                        }
                        break;
                }
            }
        }
        public class MouseLeave
        {
            public void mouseLeave(TextBox txtMouseLeave, ToolTip ttpMouseLeave)
            {
                ttpMouseLeave.Hide(txtMouseLeave);
            }
        }
        public class ToolTipRemove
        {
            public void removeToolTip(ToolTip ttpRemove, TextBox txtRemove)
            {
                ttpRemove.AutoPopDelay = 0;
                ttpRemove.InitialDelay = 0;
                ttpRemove.ReshowDelay = 0;
                ttpRemove.IsBalloon = false;
                ttpRemove.Hide(txtRemove);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox3.Text != string.Empty && comboBox1.SelectedIndex != 0)
            {
                MessageBox.Show("Valid");
            }
        }
        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            requiredField.Requiredfield(textBox1, toolTip2);
        }
        private void textBox2_Validating(object sender, CancelEventArgs e)
        {
            requiredField.Requiredfield(textBox2, toolTip3);
        }
        private void textBox1_MouseHover(object sender, EventArgs e)
        {
            objMouseover.mouseOver(textBox1, toolTip1);
        }
        private void textBox1_MouseLeave(object sender, EventArgs e)
        {
            objMouseleave.mouseLeave(textBox1, toolTip1);
        }
        private void textBox2_MouseHover(object sender, EventArgs e)
        {
            objMouseover.mouseOver(textBox2, toolTip1);
        }
        private void textBox2_MouseLeave(object sender, EventArgs e)
        {
            objMouseleave.mouseLeave(textBox2, toolTip1);
        }
        private void textBox3_MouseLeave(object sender, EventArgs e)
        {
            objMouseleave.mouseLeave(textBox3, toolTip1);
        }
        private void textBox3_MouseHover(object sender, EventArgs e)
        {
            objMouseover.mouseOver(textBox3, toolTip1);
        }
        private void comboBox1_MouseHover(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                toolTip1.AutoPopDelay = 2000;
                toolTip1.InitialDelay = 1000;
                toolTip1.ReshowDelay = 500;
                toolTip1.IsBalloon = true;
                toolTip1.ShowAlways = true;
                toolTip1.SetToolTip(comboBox1, "Select One ");
                toolTip1.Show("Select One ", comboBox1, comboBox1.Width, comboBox1.Height / 10);
            }
            else
            {
                toolTip1.ShowAlways = false;
                toolTip1.Hide(comboBox1);
            }
        }
        private void comboBox1_MouseLeave(object sender, EventArgs e)
        {
            toolTip5.Hide(comboBox1);
        }
        private void textBox3_Validating(object sender, CancelEventArgs e)
        {
            requiredField.Requiredfield(textBox3, toolTip4);
        }
    }
}

erver'>

Similar Articles