A .NET 2.0 tooltip provider

The ProvideProperty attribute and the IExtenderProvider interface allows a provider Component to provide properties for other Components. These added properties will then appear on the Visual Studio Designer to be set and even localized just like any other property. This is very useful to create helper components such as a tooltip provider because it needs to provide a different tooltip text for every Component.

The problems

  1. The ProvideProperty attribute works in a way that allows only one receiver type per property name.
  2. Properties provided to the Component type won’t appear in the Visual Studio 2005 designer.
  3. The ToolStripItem and Control classes both inherit from Component directly (and they don’t inherit from another common type).

These three design characteristics (which could arguably be called flaws) impose us a certain limitation. To provide a tooltip to both Controls and ToolStripItems with the same TooltipProvider, we need to provide two separate  properties (each with one getter and one setter).

Furthermore, we can only implement one IExtenderProvider interface, so its CanExtend method needs to return true for both Controls and ToolStripItems independently of the property provided. In the Visual Studio designer, it does not pose any problem if the CanExtend method returns true when the client checks if the property provider for the Control property supports a receiver object of type ToolStripItem (or the other way aroung). Thus, the CanExtend method needs to look this way:

public bool CanExtend(object extendee) {
    return (extendee is Control || extendee is ToolStripItem);
}

This is a fragile solution which may break in other designers or with a different client and, in my opinion, shows a lack of consistency in the framework.

A potentially better solution would be to offer two separate tooltip provider components serving as facades to the main tool provider functionality. Unfortunately, this is more complex to maintain and more difficult to use from the end user (Visual Studio designer user) perspective.

The solution

I developed a Tooltip provider that implements this solution for a client last week and they accepted to open source the component and let me distribute it on my blog.

The following screenshot comes from an upcoming version of the application I developed it for. It is a signs and labels printing application called Signs Evolution, by Soft3d.

The code

Here is the tooltip sources and demo project (zipped: 26KB). To use this tooltip component, you must agree to the terms of the MIT licence.

Update: I updated the code example so the tooltip doesn’t appear behind the parent form when the window loses focus.

5 Responses to A .NET 2.0 tooltip provider

  1. I was using this tooltip control for button as well as for toolstripmenu… some time tooltip is coming behind the dialog… it occurs some time only button..however for toolstripmenu it happens always…

  2. I forgot to set the ParentForm property of the Tooltip component on the demo form. Without it, the tooltip doesn’t know its parent and it gets pushed back the z axis when other windows are activated.

    I just fixed this and added an example for a ToolStripItem in the demo. I also updated the code to the latest version from our production application.

    If you set the parent form in the designer for the Tooltip component, do you still have the same problem?

  3. I would like to send you an email.. Can you send me a test mail to subashjayan at gmail dot com

  4. Can you please try to add a ContextMenuStrip and try to show the tooltip on each MenuItem… It is coming behind the dialog always. I am not able to upload the source files to show you this. Thats why I requested your email id.

    Thanks in advance…

  5. I’m sorry I misunderstood you the first time. Unfortunately, I could not find a solution where the tooltip form is topmost without taking the focus away from the application. Aside from that, using SendToBack on the ToolStripDropDown doesn’t work, and neither does using BringToFront on the tooltip.

    The best I can propose if you want to show these tooltips over a ToolStripMenuItem is to set the alignment to “TopRight” and horizontal location to “RightOfControl”. This will make the tooltip appear to the right of the menu item which, from a usability point of view, seems like an even better solution.

    It may be possible to show a form over the dropdown without stealing the focus using Win32 API calls, but you will need to research them.

Leave a Reply