根据语言动态地将Tooltip分配给控件. [英] Assigning Tooltip to the control dynamically according to the language.

查看:62
本文介绍了根据语言动态地将Tooltip分配给控件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想根据语言将工具提示动态绑定到控件.
这是我的代码:

Hi,

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

/// <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:

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.

推荐答案

为您的项目创建一个字符串资源,并将工具提示的文本添加为​​对该字符串资源的引用.

您可以通过这种方式创建多种语言支持.

您的代码将如下所示:
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 :
BindToolTipsToControls(pbHeadings, Resources.Click_here_to_get_the_department);


此处的注释和代码假定您正在Windows窗体中工作,尽管我相信此处的内容应与WPF同样重要.

这里的问题是,当您执行此操作时,在"BindToolTipsToControls"中:
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();

您创建了一个新的工具提示.

这意味着当您将鼠标悬停在Contol上时,您将显示两个工具提示:.NET或WinForms中这不是缺陷:一个控件可以有多个工具提示(为什么?:最好问一下MicroSoft吗?).

解决方案很简单:不创建新的工具提示,而是重新使用已使用的工具提示:

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:

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);
 }

请注意,上面的示例假定您具有多个ToolTip:当一个ToolTip可用于Form曲面上或嵌套"在其他容器深处的控件上时,是否真的需要一个ToolTip?表格表面?

我可以建议您回顾一下ToolTip控件公开的属性和事件,以及WinForms中组件"的概念...或WPF中的等效概念,并请注意,尽管可以轻松推断出ToolTip必须维护一个内部集合.要显示一个或多个控件的文本,除了RemoveAll()方法外,您无权访问该集合.并请注意,给定一个控件,您将无法知道...例如通过访问该控件的属性...如果该控件具有ToolTip或ToolTips.

因此,诸如工具提示之类的组件通常称为Extender或Provider.

为什么工具提示控件会出现在VS Studio Pro 2010 ToolBox子菜单的公共控件"下,而不是在组件"下?最好问微软:):即使工具提示控件上的工具提示也告诉您它是一个组件.

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.


这篇关于根据语言动态地将Tooltip分配给控件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆