Create a smart Tool tip handler for winform in C#.net

How to create a samrt tooltip handler for whole controls in form in C#.net

I found the concept helpful for me. This is not a C# feature at all.

Tool tip

Tool tip is a must have for good application. C#.Net provides plenty of tools to create awesome UI. Sometimes we all want the same. Reduce the line of code and bring performance .

Suppose I have many buttons, grid, text boxes on the form and want to show some helpful information on the screen when user hover the mouse over the boxes.

It can be added using mouse hover event handles, each control should have a event handler for showing tips.

One event handle for all

Instead of creating event handler for each controls we can create a single one for all and assign to all of the controls at run time.

  private void HandleMouseHover(object sender, EventArgs e)
        {
            try
            {
                string tooltipTxt = string.Empty;
                var ctrlName = ((Control)sender)?.Name;
                switch (ctrlName)
                {
                    case "dtp_date": 
                        tooltipTxt = "By default, it is the current date,but u can change it:)";
                        break;
                    

                    default:
                        tooltipTxt = "Hover your ouse anywhere on the screen for Help :)";
                        break;
                }
                tooltip.Visible = tooltipTxt.Length > 0;
                tooltip.Text = tooltipTxt;
            }
            catch (Exception)
            {

                throw;
            }
        }

By converting sender to Control type, we can access the properties of current object in which event got fired and also able check the name and add relevant message.

Make use of a switch will help us to keep the all the events in a single handler.

Finally need to assign the handler to control in load event

private void frm_receipt_Load(object sender, EventArgs e)
        {
... rest of your code
 dtp_date.Leave += HandleMouseLeave;

}

tooltip
csharp
how to
event-handler
winform

Write your comment