Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to bind the tooltip to the control dynamically according to the language.
Here is my code:

C#
/// <summary>
/// This method will be used to bind the Tooltip to the controls.
/// </summary>
private void BindControlToolTips()
{
     BindToolTipsToControls(pbHeadings, "Click here to get the Employee details from Database.");
}

/// <summary>
/// This method will be used to bind the tooltip to the specific control.
/// </summary>
/// <param name="control">Control</param>
private void BindToolTipsToControls(Control control, string tooltipMessage)
{
     ToolTip toolTip = new ToolTip();
     toolTip.SetToolTip(control, tooltipMessage);
}


After that I want ot change the tooltip runtime like this:
C#
BindToolTipsToControls(pbHeadings, "Click here to get the Department.");


The binding works properly but the issue is the new tooltip is placed on the old tooltip and the new tooltip is shorter that old one in this case both is display.
Posted
Updated 13-Sep-11 19:40pm
v3
Comments
Simon Bang Terkildsen 14-Sep-11 1:39am    
Code block added

Create a string resource for your project and add the texts for your tooltips as references to the string resource.

You can create multiple language support this way.

And your code will look like :
C#
BindToolTipsToControls(pbHeadings, Resources.Click_here_to_get_the_department);
 
Share this answer
 
Comments
[no name] 14-Sep-11 2:31am    
perfect
Mehdi Gholam 14-Sep-11 2:49am    
Cheers
Remarks and code here assume you are working in Windows Forms, although I believe the content here should be equally relevant to WPF.

The problem here is that in 'BindToolTipsToControls' when you do this:
ToolTip toolTip = new ToolTip();
You create a new ToolTip.

That means when you mouse-hover over the Contol you are displaying two ToolTips: this is not a flaw in .NET or WinForms: a Control can have more than one ToolTip (why ? : better ask MicroSoft ?).

The solution is simple: do not create a new ToolTip, but re-use the one already being used:
C#
private void button2_Click(object sender, EventArgs e)
 {
     BindToolTipsToControls(toolTip1, label1, "another tooltip");
 }

 private void BindToolTipsToControls(ToolTip theToolTip, Control control, string tooltipMessage)
 {
     theToolTip.SetToolTip(control, tooltipMessage);
 }
Note that the above example assumes you have more than one ToolTip: is that really necessary, when one ToolTip can be used for any number of Controls on a Form surface, or on Controls "nested" deep inside other Containers on a Form surface ?

May I suggest you review the properties and Events exposed by the ToolTip Control, and the concept of a "Component" in WinForms ... or its equivalent in WPF, and note that while you can easily infer a ToolTip must maintain an internal collection of Text to display for one, or many, Controls, you have no access to that collection except the RemoveAll() method. And note that given a Control, you have no way to know ... say by accessing a property of that control ... if that Control has a ToolTip, or ToolTips.

For this reason, a Component like a ToolTip is often referred to as an Extender, or Provider.

Why does the ToolTip Control appear in the VS Studio Pro 2010 ToolBox sub-menus under "Common Controls," and not "Components" ? Better ask Microsoft :): even the tooltip on the ToolTip Control tells you it is a Component.
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900