当我在组合框中选择一种语言时,如何更改所有表单的文本?

时间:2012-10-28 01:52:00

标签: c# .net winforms localization

在改变语言时喜欢facebook,它会改变所有..需要帮助来重建我的代码。我有3个表格和我的组合框,我以前选择的语言是第一种形式。当我在第一种形式中选择组合框中的语言时,我希望我的其他两种形式也会改变它的语言。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedItem.ToString() == "English")
    {
        ChangeLanguage("en");
    }
    else if (comboBox1.SelectedItem.ToString() == "Spanish")
    {
        ChangeLanguage("es-ES");
    }
    else if (comboBox1.SelectedItem.ToString() == "French")
    {
        ChangeLanguage("fr-FR");
    }
}

private void ChangeLanguage(string lang)
{
    foreach (Control c in this.Controls)
    {
        ComponentResourceManager crm = new ComponentResourceManager(typeof(Form1));
        crm.ApplyResources(c, c.Name, new CultureInfo(lang));
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用包含" LanguageChanged"的静态事件的基类,以及触发它的函数。 如果您的所有表单都是该基类的派生类,则可以在其中处理该事件。 类似的东西:

public partial class Form1 : BaseLanguageForm
{

    public Form1()
    {
        InitializeComponent();

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem.ToString() == "English")
        {
            this.TriggerLanguageChange("en");
        }
        else if (comboBox1.SelectedItem.ToString() == "Spanish")
        {
            this.TriggerLanguageChange("es-ES");
        }
        else if (comboBox1.SelectedItem.ToString() == "French")
        {
            this.TriggerLanguageChange("fr-FR");
        }
    }


}


/// <summary>
/// The event that should be passed whenever language needs to be changed
/// </summary>
public class LanguageArgs:EventArgs
{
    string _languageSymbol;
    /// <summary>
    /// Gets the language symble.
    /// </summary>
    public string LanguageSymbol
    {
        get { return _languageSymbol; }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="LanguageArgs"/> class.
    /// </summary>
    /// <param name="symbol">The symbol.</param>
    public LanguageArgs(string symbol)
    {
        this._languageSymbol = symbol;
    }
}

/// <summary>
/// A base class that your class should derivative from
/// </summary>
public class BaseLanguageForm:Form
{
    /// <summary>
    /// Triggers the language change event.
    /// </summary>
    /// <param name="languageSymbol">The language symbol.</param>
    protected void TriggerLanguageChange(string languageSymbol)
    {
        if (Form1.onLanguageChanged != null)
        {
            LanguageArgs args = new LanguageArgs(languageSymbol);
            Form1.onLanguageChanged(this, args);
        }
    }

    /// <summary>
    /// This should be triggered whenever the combo box value chages
    /// It is protected, just incase you want to do any thing else specific to form instacne type
    /// </summary>
    protected static event EventHandler<LanguageArgs> onLanguageChanged;

    /// <summary>
    /// This will be called from your form's constuctor 
    /// (you don't need to do anything, the base class constuctor is called automatically)
    /// </summary>
    public BaseLanguageForm()
    {
        //registering to the event
        BaseLanguageForm.onLanguageChanged += new EventHandler<LanguageArgs>(BaseLanguageForm_onLanguageChanged);
    }

    /// <summary>
    /// The function that was regidtered to the event
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The e.</param>
    void BaseLanguageForm_onLanguageChanged(object sender, LanguageArgs e)
    {
        string lang = e.LanguageSymbol;
        foreach (Control c in this.Controls)
        {
            ComponentResourceManager crm = new ComponentResourceManager(typeof(Form1));
            crm.ApplyResources(c, c.Name, new CultureInfo(lang));
        }
    }

}

答案 1 :(得分:0)

根据我从您的问题中理解的内容,我认为如果您尝试从其他表单中调用ChangeLanguage(string lang),这是可能的。

您需要在每个Timer添加Form,以便使用void on和两个变量。第一个是bool,用于检测是否需要在其他两个表单中调用ChangeLanguage,第二个是string,它将指示语言。这两个变量必须是publicstatic,以便可以通过其他形式进行控制。

示例


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace NameSpaceGoesHere
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static bool _ChangeLanguage = false; //This will indicate if the void needs to be called
        public static string _Language = ""; //This will indicate our new language
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeLanguage("fr-FR");

        }

        private void ChangeLanguage(string lang)
        {
            foreach (Control c in this.Controls)
            {
                ComponentResourceManager crm = new ComponentResourceManager(typeof(Form1));
                crm.ApplyResources(c, c.Name, new CultureInfo(lang));
                _Language = lang; //Sets the language to lang
                _ChangeLanguage = true; //Indicates that the void needs to be called through the TWO other forms as well
            }
        }

    }
}

Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace NameSpaceGoesHere
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public static bool _ChangeLanguage = false; //Required for the third form
        private void Form2_Load(object sender, EventArgs e)
        {
            Timer Timer1 = new Timer(); //Initialize a new Timer object
            Timer1.Tick +=new EventHandler(Timer1_Tick); //Link its Tick event with Timer1_Tick
            Timer1.Start(); //Start the timer
        }
        private void ChangeLanguage(string lang)
        {
            foreach (Control c in this.Controls)
            {
                ComponentResourceManager crm = new ComponentResourceManager(typeof(Form1));
                crm.ApplyResources(c, c.Name, new CultureInfo(lang));
                _ChangeLanguage = true;
            }
        }
        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (Form1._ChangeLanguage)  //Indicates if ChangeLanguage() requires to be called
            {
                Form1._ChangeLanguage = false; //Indicates that we'll call the method
                ChangeLanguage(Form1._Language); //Call the method
            }
        }
    }
}

Form3.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace NameSpaceGoesHere
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        private void Form3_Load(object sender, EventArgs e)
        {
            Timer Timer1 = new Timer(); //Initialize a new Timer object
            Timer1.Tick += new EventHandler(Timer1_Tick); //Link its Tick event to Timer1_Tick
            Timer1.Start(); //Start the timer
        }
        private void ChangeLanguage(string lang)
        {
            foreach (Control c in this.Controls)
            {
                ComponentResourceManager crm = new ComponentResourceManager(typeof(Form1));
                crm.ApplyResources(c, c.Name, new CultureInfo(lang));
            }
        }
        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (Form2._ChangeLanguage) //Indicates if ChangeLanguage() requires to be called
            {
                Form2._ChangeLanguage = false; //Indicates that we'll call the method
                ChangeLanguage(Form1._Language); //Call the method
            }
        }
    }
}

谢谢, 我希望你觉得这很有帮助:)