0

I am developing an app on winforms which contains many controls. I want the application to be in multiple languages and the text of the buttons and labels change correctly. The problem is that in the first change of language I make (from default to spanish/english), the buttons and labels change their position and size, but I would like them to maintain as they were in the initial way. Do someone know why could this be?

This is the code I use:

private void English_ToolStrip_Click(object sender, EventArgs e)
        {
            English_ToolStrip.Checked = true;
            Spanish_ToolStrip.Checked = false;
            ChangeLanguage(typeof(Form1), "en");
        }

private void ChangeLanguage(Type t, string language)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language);
            foreach (Control c in Controls)
            { 
                resources.ApplyResources(c, c.Name, new CultureInfo(language));
            }
            foreach (ToolStripItem item in toolStrip1.Items)
            {
                if (item is ToolStripDropDownItem)
                    foreach (ToolStripItem dropDownItem in ((ToolStripDropDownItem)item).DropDownItems)
                    {
                        resources.ApplyResources(dropDownItem, dropDownItem.Name, new CultureInfo(language));
                    }
                resources.ApplyResources(item, item.Name, new CultureInfo(language));
            }
        }

I think I might be missing something when I initialize the form because the change of size and location only happens when I first change the language. After that, it doesn't matter to which language I change or how many times but the size and locations maintain the same.

These are the controls before changing the language

These are the controls after changing the language

Leire
  • 1
  • 1
  • 1
    Check if there are multipe resource files (`form.en.resx` and `form.en-US.resx` are different ones). In winforms designer you can [change form language](https://stackoverflow.com/a/32990088/1997232) and that would load (and save) all data from a corresponding resource file. The control `Location` and `Size` may be different for each language. – Sinatr Apr 30 '21 at 09:37
  • Is it that the button text is actually bigger, and therefore requires more room? – Neil Apr 30 '21 at 09:40
  • @Sinatr I have three resource files: Form1.resx, Form1.en.resx and Form1.es.resx, they were generated automatically when I changed the texts for each language. I have looked and the Location and Size are the same for all the languages. – Leire Apr 30 '21 at 10:02
  • @Neil I have tried with different button texts and the size change happens always, even when the text is shorter. – Leire Apr 30 '21 at 10:04
  • Can you link to a picture that shows the change in size (before & after)? – Neil Apr 30 '21 at 12:42
  • @Neil I have just added the pictures in the description of the question. – Leire May 03 '21 at 08:24
  • I think here is something that is changing the font (or font size) in the new language. Could be some default in the resource file, or some other code doing it? – Neil May 03 '21 at 08:27
  • @Neil I haven't changed anything in the configuration of the controls, just the text. I'll look in the resource file if there is some change. – Leire May 03 '21 at 09:21
  • @Neil I have just tried to call the ChangeLanguage() function in the Load event of the form and the controls appear in the "big" way from the beginning, so, when I change the language again the size and position maintain the same. I will work with the form this way, so I do not need more answers. Thank you for all!! :) – Leire May 03 '21 at 10:19

0 Answers0