WinForms strings in resource files, wired up in designer

后端 未结 5 2054
星月不相逢
星月不相逢 2021-02-05 12:58

I\'m trying to localise a WinForms app for multiple languages. I\'m trying to find a way to set my form labels/buttons text properties to read from the resources file in the des

5条回答
  •  终归单人心
    2021-02-05 13:11

    The only way I can think of would be to create a custom control that would add a property for the resource name. When the property is set, grab the value from the project resource file and set the text property with it. You will want to make sure that Text doesn't get serialized or it might overwrite the value set by ResourceName.

    public class ResourceLabel
        : Label
    {
        private string mResourceName;
        public string ResourceName
        {
            get { return mResourceName; }
            set
            {
                mResourceName = value;
                if (!string.IsNullOrEmpty(mResourceName))
                    base.Text = Properties.Resources.ResourceManager.GetString(mResourceName);
            }
        }
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text
        {
            get { return base.Text; }
            set 
            { 
                // Set is done by resource name.
            }
        }
    }
    

提交回复
热议问题