如何设置文本从资源文件在设计时的控制? [英] How to set text to a control from resource file in design time?

查看:189
本文介绍了如何设置文本从资源文件在设计时的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否存在一种方法,在设计时的资源文件中设置控件的文本属性:

I want to know if there exists a way to set a control's Text property from a resource file in design time:

或者只能进行编程这个过程?

Or this process can only be performed programatically?

推荐答案

设计师只为序列化文本属性字符串。您不能直接使用设计师Text属性设置为一个资源价值。

The designer only serializes string for Text property. You can not set the Text property to a resource value directly using designer.

即使你打开 Form1.Designer.cs 文件,并添加一行初始化设置文本属性,如资源值 Resource1.Key1 ,在设计师的第一个改变之后,设计师通过设置字符串替换代码该资源的价值文本属性

Even if you open the Form1.Designer.cs file and add a line to initialization to set the Text property to a resource value like Resource1.Key1, after first change in designer, the designer replace your code by setting the string value of that resource for Text property.

在一般我建议使用标准的的Windows窗体的本地化机制,使用本地化语言 表格的属性。

In general I recommend using standard localization mechanisms of windows forms, using Localizable and Language property of Form.

但如果由于某种原因,你想使用你的资源文件,并希望使用基于设计的解决方案,为不错的选择,你可以创建一个的扩展组件来为您的设计时控制的资源键,然后在运行时使用它。

But if in some reason you want to use your resource file and want to use a designer-based solution, as good option you can create an extender component to set the resource key for your control at design-time and then use it at run-time.

代码的扩展组件在帖子的结尾。

用法

请确保您有一个资源文件。例如 Resources.resx 在属性文件夹中。另外,还要确保你在资源文件中的一些资源键/值。对于价值=值1例如密钥1,密钥2与价值=值2。然后:

Make sure you have a resource file. For example Resources.resx in the properties folder. Also make sure you have some resource key/value in the resource file. For example Key1 with value = "Value1", Key2 with value = "Value2". Then:


  1. 将一个 ControlTextExtender 组件表单上

  2. 使用属性网格设置它的 ResourceClassName 属性,例如WindowsApplication1.Properties.Resources`
    <一个资源文件的全名HREF =htt​​p://i.stack.imgur.com/SnAVm.png相对=nofollow>

  3. 选择您要设置的每个控制它的文本并使用属性网格设置的值的ResourceKey上controlTextExtender1 属性设置为你想要的资源键。

  1. Put a ControlTextExtender component on your form.
  2. Using property grid set the ResourceClassName property of it to the full name of your resource file for example WindowsApplication1.Properties.Resources`
  3. Select each control you want to set its Text and using property grid set the value of ResourceKey on controlTextExtender1 property to the resource key that you want.

然后运行该应用程序并查看结果。

Then run the application and see the result.

结果

和这里是结果的截图,正如你看到的,我甚至。本地化文本形式的财产这样

and here is an screenshot of result, and as you see, I even localized Text property of the form this way.

在运行时文化之间切换

您可以在运行时文化之间切换,而无需关闭并重新打开只需使用形式:

You can switch between cultures at run-time, without need to close and reopen the form simply using:

System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fa");
this.controlTextExtender1.EndInit();



实施

下面是一个基本的实现思路:

Here is a basic implementation of the idea:

[ProvideProperty("ResourceKey", typeof(Control))]
public class ControlTextExtender 
    : Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
{
    private Hashtable Controls;
    public ControlTextExtender() : base() { Controls = new Hashtable(); }

    [Description("Full name of resource class, like YourAppNamespace.Resource1")]
    public string ResourceClassName { get; set; }

    public bool CanExtend(object extendee)
    {
        if (extendee is Control)
            return true;
        return false;
    }

    public string GetResourceKey(Control control)
    {
        return Controls[control] as string;
    }

    public void SetResourceKey(Control control, string key)
    {
        if (string.IsNullOrEmpty(key))
            Controls.Remove(control);
        else
            Controls[control] = key;
    }

    public void BeginInit() { }

    public void EndInit()
    {
        if (DesignMode)
            return;

        var resourceManage = new ResourceManager(this.ResourceClassName, 
                                                 this.GetType().Assembly);
        foreach (Control control in Controls.Keys)
        {
            string value = resourceManage.GetString(Controls[control] as string);
            control.Text = value;
        }
    }
}

这篇关于如何设置文本从资源文件在设计时的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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