<tfoot id='v0Cx0'></tfoot><legend id='v0Cx0'><style id='v0Cx0'><dir id='v0Cx0'><q id='v0Cx0'></q></dir></style></legend>

  1. <small id='v0Cx0'></small><noframes id='v0Cx0'>

        <bdo id='v0Cx0'></bdo><ul id='v0Cx0'></ul>
      <i id='v0Cx0'><tr id='v0Cx0'><dt id='v0Cx0'><q id='v0Cx0'><span id='v0Cx0'><b id='v0Cx0'><form id='v0Cx0'><ins id='v0Cx0'></ins><ul id='v0Cx0'></ul><sub id='v0Cx0'></sub></form><legend id='v0Cx0'></legend><bdo id='v0Cx0'><pre id='v0Cx0'><center id='v0Cx0'></center></pre></bdo></b><th id='v0Cx0'></th></span></q></dt></tr></i><div id='v0Cx0'><tfoot id='v0Cx0'></tfoot><dl id='v0Cx0'><fieldset id='v0Cx0'></fieldset></dl></div>

      如何在设计时将文本从资源文件设置为控件?

      时间:2023-08-27

            <legend id='bsjSo'><style id='bsjSo'><dir id='bsjSo'><q id='bsjSo'></q></dir></style></legend>

          • <small id='bsjSo'></small><noframes id='bsjSo'>

          • <i id='bsjSo'><tr id='bsjSo'><dt id='bsjSo'><q id='bsjSo'><span id='bsjSo'><b id='bsjSo'><form id='bsjSo'><ins id='bsjSo'></ins><ul id='bsjSo'></ul><sub id='bsjSo'></sub></form><legend id='bsjSo'></legend><bdo id='bsjSo'><pre id='bsjSo'><center id='bsjSo'></center></pre></bdo></b><th id='bsjSo'></th></span></q></dt></tr></i><div id='bsjSo'><tfoot id='bsjSo'></tfoot><dl id='bsjSo'><fieldset id='bsjSo'></fieldset></dl></div>

              <tbody id='bsjSo'></tbody>
              <bdo id='bsjSo'></bdo><ul id='bsjSo'></ul>

                <tfoot id='bsjSo'></tfoot>
              1. 本文介绍了如何在设计时将文本从资源文件设置为控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想知道是否有办法在设计时从资源文件中设置控件的 Text 属性:

                或者这个过程只能以编程方式执行?

                解决方案

                设计器只序列化 Text 属性的字符串.您不能直接使用设计器将 Text 属性设置为资源值.

                即使您打开 Form1.Designer.cs 文件并在初始化中添加一行以将 Text 属性设置为像 Resource1.Key1 这样的资源值,在第一次更改设计器后,设计器通过为 Text 属性设置该资源的字符串值来替换您的代码.

                一般来说,我建议使用标准

              2. 选择要设置其 Text 的每个控件,并使用属性网格将 ResourceKey on controlTextExtender1 属性的值设置为所需的资源键.
              3. 然后运行应用程序并查看结果.

                结果

                这是结果的截图,如你所见,我什至以这种方式本地化了表单的 Text 属性.

                在运行时切换文化

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

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

                实施

                这是该想法的基本实现:

                [ProvideProperty("ResourceKey", typeof(Control))]公共类 ControlTextExtender:组件,System.ComponentModel.IExtenderProvider,ISupportInitialize{私有哈希表控件;公共 ControlTextExtender() : base() { Controls = new Hashtable();}[Description("资源类全名,如YourAppNamespace.Resource1")]公共字符串 ResourceClassName { 获取;放;}公共布尔 CanExtend(对象扩展对象){if (extendee is Control)返回真;返回假;}public string GetResourceKey(控制控件){以字符串形式返回 Controls[control];}public void SetResourceKey(控制控件,字符串键){if (string.IsNullOrEmpty(key))Controls.Remove(控制);别的控制[控制] = 键;}公共无效 BeginInit() { }公共无效 EndInit(){如果(设计模式)返回;var resourceManage = new ResourceManager(this.ResourceClassName,this.GetType().Assembly);foreach(Controls.Keys 中的控制控件){字符串值 = resourceManage.GetString(Controls[control] as string);control.Text = 值;}}}

                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?

                解决方案

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

                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.

                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.

                Code for the extender component is at the end of the post.

                Usage

                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. 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.

                Result

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

                Switch between Cultures at Run-Time

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

                Implementation

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

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

                上一篇:MessageBox 按钮 - 设置语言? 下一篇:查找所有源硬编码字符串

                相关文章

                最新文章

                1. <tfoot id='WghqM'></tfoot>

                  1. <legend id='WghqM'><style id='WghqM'><dir id='WghqM'><q id='WghqM'></q></dir></style></legend>

                    <i id='WghqM'><tr id='WghqM'><dt id='WghqM'><q id='WghqM'><span id='WghqM'><b id='WghqM'><form id='WghqM'><ins id='WghqM'></ins><ul id='WghqM'></ul><sub id='WghqM'></sub></form><legend id='WghqM'></legend><bdo id='WghqM'><pre id='WghqM'><center id='WghqM'></center></pre></bdo></b><th id='WghqM'></th></span></q></dt></tr></i><div id='WghqM'><tfoot id='WghqM'></tfoot><dl id='WghqM'><fieldset id='WghqM'></fieldset></dl></div>

                    <small id='WghqM'></small><noframes id='WghqM'>

                      <bdo id='WghqM'></bdo><ul id='WghqM'></ul>