3

就像 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));
    }
}
4

2 回答 2

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

}
于 2012-10-28T07:57:17.637 回答
0

根据我从您的问题中了解到的情况,我认为如果您也尝试ChangeLanguage(string lang)从其他表格中拨打电话,这是可能的。

您需要Timer添加到每个Form您想使用 void 的变量和两个变量。第一个是bool用于检测是否ChangeLanguage需要在其他两种形式中调用的 a,第二个是string用于指示语言的 a。这两个变量必须是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
            }
        }
    }
}

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

于 2012-10-28T07:38:51.830 回答