I am having trouble with this, What i am trying to achieve is the following: I have a main form which is the startup form (MDI Container) and then a few forms that are the child forms, what i am trying to achieve is the following, this is code that is happening on every screen at the moment, and i want to build it into a class but, i don't know how to pass this (the click events happen on every form at the moment, and i want it to run of a menu click on the MDI Parent(container):
UPDATE
I have gotten it to work with all the ChildForms thanx @JamesBarras this is how my code works now, i have remove the button click events, for it is only running from a MenuStripItem_Click Event
#region Change Language
private void englishToolStripMenuItem_Click(object sender, EventArgs e) {
Class1 cls = new Class1();
/// This is for Main and all the forms of MainForms Children
foreach (var childForm in this.MdiChildren) {
cls.ChangeLanguage(sender, log, childForm, this, this.menuStrip, "en");
//ChangeLanguage("en", childForm);
}
}
private void arabicToolStripMenuItem_Click(object sender, EventArgs e) {
Class1 cls = new Class1();
/// This is for Main and all the forms of MainForms Children
foreach (var childForm in this.MdiChildren) {
cls.ChangeLanguage(sender, log, childForm, this, this.menuStrip, "ar");
//ChangeLanguage( "ar", childForm);
}
}
#endregion
I just created this class as this code was running on the same page as the click event as a Method, I want the class that i am using/created (ChangeLanguage) to know what this is,because on the normal form, i can say this but in a class it doesn't know what the this is on the form that i have my click events, here is my code in the class that i created:
UPDATE
I have fine tuned the Class to do exactly what i need it to do, yet, i still can't translate the MenuStripItems and i am getting the value of this but still no menustrip Translate only the direction switch works
namespace languageChange.Classes { class Class1 {
#region Methods
/// <summary>
/// This is mainly to change the language and the layout direction
/// </summary>
/// <param name="sender"></param>
/// <param name="log"></param>
/// <param name="form"></param>
/// <param name="thiss"></param>
/// <param name="strip"></param>
/// <param name="lang"></param>
public void ChangeLanguage(object sender, Logger log, Form form, Form thiss, Control strip, string lang) {
string senderText = sender.GetType().ToString();
RightToLeft direction = RightToLeft.No;
if (lang == "ar") {
direction = RightToLeft.Yes;
}
thiss.RightToLeft = direction;
CultureInfo CurrentLocale = new CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
foreach (Control childForm in form.Controls) {
ComponentResourceManager resources = new ComponentResourceManager(form.GetType());
resources.ApplyResources(childForm, "$this");
childForm.RightToLeft = direction;
if (log.isDebugEnabled) log.Debug("--------------------------------------> c = " + childForm.Name);
RefreshResources(log, lang, childForm, resources, CurrentLocale, strip);
}
}
/// <summary>
/// This is to for the Refresh all the Resources
/// </summary>
/// <param name="log"></param>
/// <param name="lang"></param>
/// <param name="ctrl"></param>
/// <param name="resources"></param>
/// <param name="CurrentLocale"></param>
/// <param name="strip"></param>
private static void RefreshResources(Logger log, string lang, Control ctrl, ComponentResourceManager resources, CultureInfo CurrentLocale, Control strip) {
ctrl.SuspendLayout();
resources.ApplyResources(ctrl, ctrl.Name, CurrentLocale);
foreach (Control control in ctrl.Controls) {
RefreshResources(log, lang, control, resources, CurrentLocale, strip); // recursion
if (strip is ToolStrip) {
RefreshResources(((ToolStrip)strip).Items, resources, CurrentLocale);
}
ctrl.ResumeLayout(true);
if (log.isTraceEnabled) log.Trace("c=" + ctrl.Name);
}
}
/// <summary>
/// Which is done here [Refer to previous Summary]
/// </summary>
/// <param name="col"></param>
/// <param name="resources"></param>
/// <param name="CurrentLocale"></param>
private static void RefreshResources(ToolStripItemCollection col, ComponentResourceManager resources, CultureInfo CurrentLocale) {
foreach (ToolStripMenuItem item in col) {
if (item is ToolStripMenuItem) {
RefreshResources(((ToolStripMenuItem)item).DropDownItems, resources, CurrentLocale);
}
resources.ApplyResources(item, item.Name, CurrentLocale);
}
}
#endregion }}
UPDATE
So i got everything to translate, and change direction except for the MenuStripItems and the Menustrip as well.
Please Help me as this is really important and i am struggling.
This is a Winforms application using Visual Studio 2013 Ultimate, with C#.
Don't mind the logger, it is something i just added to log the trace and debugger
Change the signature of
ChangeLanguage(string lang)
toChangeLanguage(Form form, string lang)
so that you can pass it the form you wish to alter.subsequently you now need to refer to the form you are working on as the passed in one. So replace
this
in that method withform
and the forms typetypeof(Form1)
withform.GetType()
Everywhere you want to change language on any for you can now call
where
this
is the current formTo change language for an MDIParent you'll need to this:
Note that to change the language of the MenuStrip itself you'll need to add an overload of RefreshResources to cope with ToolStripItemCollections (they are component based not control based)
and add the following code to the original
RefreshResources