按钮工具提示未本地化 [英] Button tooltip not getting localized

查看:77
本文介绍了按钮工具提示未本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展的按钮类,如下所示;

I have an extended button class as follows;

class ExtendedButton : Button
{
    private ToolTip _tooltip = new ToolTip();

    public ExtendedButton()
    {
        _tooltip.SetToolTip(this, StringResources.MyLocalizedTooltipString);
    }
}

在上面的代码中,"StringResources.MyLocalizedTooltipString"包含各种语言的字符串.但是,当我更改线程区域性时,工具提示文本不会更改.如何实现呢? 任何帮助将不胜感激.

In above code, 'StringResources.MyLocalizedTooltipString' contains strings for various languages. But when I change thread culture, tooltip text does not get changed. How to achieve that? Any help would be appreciated.

推荐答案

这很正常. SetToolTip方法接受字符串,它将在调用SetToolTip时显示基于当前区域性从资源中提取的文本,并且在运行时更改区域性将对其没有任何影响. 所以在这里,由于设置工具提示是在按钮的构造函数中执行的,因此将使用此时的线程区域性.

It's normal. SetToolTip method accepts string and it will show the text that extracted from resource based on the current culture when calling SetToolTip and changing the culture at run-time will not have any impact on it. So here since setting tooltip is performed in constructor of your button, then the culture of thread at that moment will be used.

如果希望工具提示自动自动动态使用当前区域性,则可以选择将一个虚拟文本设置为工具提示(以启用工具提示),然后处理工具提示的Popup事件,将本地化值分配给工具提示:

If you want your tooltip dynamically use the current culture automatially, as an option you can set a dummy text as tooltip (to enable the tooltip) and then handling Popup event of tooltip, assign the localized value to the tooltip:

class ExtendedButton : Button
{
    private ToolTip _tooltip = new ToolTip();

    public ExtendedButton()
    {
        _tooltip.Popup += new PopupEventHandler(_tooltip_Popup);
        _tooltip.SetToolTip(this, "DUMMYTEXT");
    }

    void _tooltip_Popup(object sender, PopupEventArgs e)
    {
        if (_tooltip.GetToolTip(this) != StringResources.MyLocalizedTooltipString)
            _tooltip.SetToolTip(this, StringResources.MyLocalizedTooltipString);
    }
}

注意:如果您的目标不是使工具提示可以动态地本地化,而您只希望使用可本地化的工具提示(即FormLocalizable属性的工作方式),请转到ExtendedButton组件的设计器并设置Localizable属性到true,然后针对不同的提示使用不同的工具提示文本.

Note: If your goal is not making the tooltip dynamically localizable and you only want a localizable tooltip, the way Localizable property of Form works, go to designer of your ExtendedButton component and set Localizable property to true and then use different tooltip texts for different cltures.

但是请记住,创建组件后,该值不能在运行时进行动态更改.

But keep in mind, the value can not be changed dunamically at runtime, after the component is created.

这里是一个例子:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fa-IR");
var f = new Form();
f.Controls.Add(new ExtendedButton());
f.Show();

这篇关于按钮工具提示未本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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