user609511
user609511

Reputation: 4261

c# WinForm how to retrieve value from resource file on code behind

In asp.net I use like this:

  gridView_Desti.Columns["CODE_DEST"].Caption = (string) HttpContext.GetGlobalResourceObject("Client", "Code_Dest");

How can I do the same thing on WinForm ?

Client is the resource name file --> Client.resx
Code_Dest is string on Client.resx --> string Code_Dest, value Code Destinataire

Upvotes: 0

Views: 5421

Answers (4)

MC9000
MC9000

Reputation: 2403

If you don't have the namespace in, then prepend with "Properties" C# as so:

Properties.Resources1.YourResourcesName

Makes your code so much cleaner using the resx file. As an example, I have a DataGridViewImageColumn and assigned an image to it (from VS Image Library - the image is a .png file):

colAddNewItem.Image = Properties.Resource1.Add_16x;

FYI, in VB.Net it's

Resources.Resources1.YourResourcesName

There are many other ways, but this is the simplest, cleanest & preferred method.

Upvotes: 0

kerrubin
kerrubin

Reputation: 1616

You can do :

Client.ResourceManager.GetString("Code_Dest");

Depending on the culture, it will look for the string in Client.en-US.resx (if en-US is your current culture) and if it fail, in Client.resx.

You can also acces like this (Code_Dest must be in Client.resx) :

Client.Code_Dest;

Upvotes: 2

Agung Gugiaji
Agung Gugiaji

Reputation: 121

  1. Add new item -> Resources i.e 'Resources1.resx'
    1. Put necessary Resources Name & value ie string resources -> 'YourResourcesName' value whatever.
    2. Access its value with Resources1.YourResourcesName

Hope this help,

Upvotes: 0

Marek Dzikiewicz
Marek Dzikiewicz

Reputation: 2884

You should have an auto-generated class called Resources in the Properties namespace of your project. Each resource is exposed as a property in that class.

Upvotes: 3

Related Questions