0

I was playing with the ComponentResourceManager in a small test application because I need to fix the localization that was formerly automatically generated in an existing, larger, Winforms app to be manually implemented--so that all of the strings that were manually inserted into the resource files don't get nuked whenever someone opens the form designer.

For the sake of this test, on the form there is a button control. I want to set button properties using ones stored in a resource file. If I seek the properties and set them directly, this works fine.

ResourceManager resourceManager = new ResourceManager("LocalizationTest1.buttonProps", typeof(Form1).Assembly);
button1.Text = resourceManager.GetString("button1.Text");
button1.Location = (Point)(resourceManager.GetObject("button1.Location"));

However, attempting to use ApplyResources, as follows, does not work.

ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(Form1));
componentResourceManager.ApplyResources(this.button1, "button1");

It may also be relevant that the resource names button1.Text and button1.Location both have the resource name 'button1...' is not a valid identifier next to them in the resource designer.

I haven't been able to find anything relevant online thus far, so any thoughts that may be of assistance would be greatly appreciated.

Justin
  • 330
  • 1
  • 2
  • 9
  • For me, [it works](https://stackoverflow.com/a/46017814/3110834). Did you change the culture before applying the resources to see the result? Are you sure your form is [localizable](https://stackoverflow.com/a/32990088/3110834)? – Reza Aghaei Dec 12 '17 at 19:10
  • The form is localizable because it will change languages to the appropriate resx file if I change the CurrentUICulture, but only if I'm addressing the properties directly; i.e. `button1.Text = resourceManager.GetString("button1.Text");`. and not trying to address the object more broadly using ApplyResources – Justin Dec 12 '17 at 19:46
  • Have you tried the exact code which I shared in the [example](https://stackoverflow.com/a/46017814/3110834)? – Reza Aghaei Dec 12 '17 at 19:48
  • I have and unfortunately it does not work for me either. At this juncture I almost expect it's something that I'm doing wrong with the resource names. – Justin Dec 12 '17 at 20:02
  • Are you creating resources manually yourself? – Reza Aghaei Dec 12 '17 at 20:13
  • I am creating them manually. As far as I know, that is the only way to localize and change text programatically. Otherwise, the additional strings get overwritten by the resource generator, which is the problem that I am trying to solve. – Justin Dec 12 '17 at 20:24
  • That warning is relevant. Sounds that the .resx file formerly associated with Form1 is no longer associated with the form. I'd imagine you did something like renaming the file. Which breaks ComponentResourceManager, it goes looking for resources that match typeof(Form1). You'd have to implement your own component resource manager. Character-building exercise, use the Reference Source. – Hans Passant Dec 13 '17 at 15:00

1 Answers1

0

Should have probably realized this when the component resource manager doesn't take a resource file name or root name as an argument--name your resources base names the same as your form. i.e. Form1.cs, Form1.resx, Form1.fr.resx, etc.

That's it.

Thanks Hans for the "Sounds that the .resx file formerly associated with Form1 is no longer associated with the form.. I'd imagine you did something like renaming the file." comment. It didn't spell out the solution, but it helped get me there. I hope this helps someone else who also didn't come to this realization sooner.

Justin
  • 330
  • 1
  • 2
  • 9