Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form resources usage for programmatic strings?

Had a basic WinForm question: By default a resx file is created for every form or user control (along with the designer.cs). This resx works fine for all the controls and the text added to the controls via the UI.

I was wondering if I could use the same resx to add strings which have to be used programmatically and based on conditions, attached to the controls? Will the resx get overridden in any case and this custom strings be removed? What is the best practice to follow in this case?

like image 708
bschandramohan Avatar asked Nov 19 '09 06:11

bschandramohan


People also ask

What is resources Resx?

The . resx resource file format consists of XML entries that specify objects and strings inside XML tags. One advantage of a . resx file is that when opened with a text editor (such as Notepad) it can be written to, parsed, and manipulated.

How are RESX files generated?

Resource files (. resx) are only generated with source code when building a Project in Developer Studio. Resource files (. resx) are only generated with source code when building a Project in OpenEdge Architect.

What is RESX file in VB NET?

Net Resource (. resx) files are a monolingual file format used in Microsoft . Net Applications. The . resx resource file format consists of XML entries, which specify objects and strings inside XML tags.


1 Answers

There's a strange problem with the string resources in the Resources.resx file. There's no obvious way that I ever discovered how to create a new resource table for another language with the IDE. It can be done by hand though. Follow these steps:

  1. Project + Properties, Resource tab, add the strings you want to use in your program
  2. Start Windows Explorer and navigate to your project's Properties folder
  3. Copy and paste the Resources.resx file
  4. Rename it to the culture you want to use. For example: Resources.fr-FR.resx
  5. Back to VS, click the Show All Files icon in the Solution Explorer window
  6. Expand the Properties node, the new resource file should be visible
  7. Right-click it and select "Include in project"
  8. Select it, in the Properties window set Custom Tool = "ResXFileCodeGenerator"
  9. Verify that Build Action is set to "Embedded Resource"

Build your program. You should get a new folder in your project's bin\Debug directory with the satellite assembly, named projectname.resources.dll. This satellite assembly contains both the localized strings and any localized resource from the forms. Test that it works with code like this:

public Form1() {
  System.Threading.Thread.CurrentThread.CurrentUICulture =
    System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
  InitializeComponent();
  textBox1.Text = Properties.Resources.String1;
}
like image 84
Hans Passant Avatar answered Oct 13 '22 21:10

Hans Passant