简体   繁体   中英

How can I change all the form's text when I choose a language in combobox?

Like facebook when changing its language, it change all.. need help to rebuild my codes here. I have 3 forms and my combobox where I used to choose the language is in the 1st form. When I choose language in combobox in my 1st form, I want my other 2 forms will change its language also.

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

You can use a base class, that holds a static event of "LanguageChanged", and a function to trigger it. If all your forms are derivative class of that base class, the event could be handled within it. Something like:

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

}

Based on what I've understood from your question, I think this is possible if you are trying to call ChangeLanguage(string lang) from the other forms as well.

You'll need a Timer added to every Form you would like to use the void on, and TWO variables. The first is a bool which will be used to detect if ChangeLanguage is required to be called within the other TWO forms, and the second is a string which will indicate the language. These TWO variables must be public and static so that they can be controlled through other forms.

Example


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

Thanks,
I hope you find this helpful :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM